这节课学习了链接数据库的增删改查,所有的变化都会同步到数据库中。跟着老师做中发现了几个问题 2.老师多次强调把连接数据库的代码设置成全局变量,这样就不用每个函数里面都打一次了,但是当进行更新操作时,如果删除函数内部没有这行代码就只能进行一次修改,不能连续修改。相反,添加此代码即可多次更改。 老师只是给我们进行了简单的举例,很多地方还需要我们自己去优化。从今天开始要开始完成大作业,fighting! 以下是本次课讲解的基本增删改查的代码,功能性对来说并不齐全(例如只能修改姓名等)
1.要运行的时候突然拦截了我的程序,说是有木马,给我吓一跳,我做的可是正经程序,后来查了一下看到了这个博客:HEUR/Malware.QVM03.Gen木马是360专有的误报
好的,那没事了。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Login { public partial class FormMain : Form { private object mrDavidDataSet; public FormMain() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } private void FormMain_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“sCHOOLDataSet.Student”中。您可以根据需要移动或删除它。 this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student); } SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=1234"); private void insert_Click(object sender, EventArgs e)//增加 { String StuID = textBox1.Text.Trim(); String StuName = textBox2.Text.Trim(); String StuSex = textBox3.Text.Trim(); String StuAge = textBox4.Text.Trim(); String StuSdept = textBox5.Text.Trim(); try { con.Open(); string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept)" + "VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuAge + "'," + StuSdept +")"; SqlCommand cmd = new SqlCommand(insertStr, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("输入数据违反要求!"); } finally { con.Dispose(); } this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student); } private void delete_Click(object sender, EventArgs e)//删除 { try { con.Open(); string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句 SqlCommand cmd = new SqlCommand(delete_by_id, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("请正确选择行!"); } finally { con.Dispose(); } this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student); } private void update_Click(object sender, EventArgs e)//修改 { String StuID = textBox1.Text.Trim(); String StuName = textBox2.Text.Trim(); //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=1234"); try { con.Open(); string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'"; SqlCommand cmd = new SqlCommand(insertStr, con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("输入数据违反要求!"); } finally { con.Dispose(); } this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student); } private void select_Click(object sender, EventArgs e)//查询 { String StuID = textBox1.Text.Trim(); try { con.Open(); String select_by_id = "select * from Student where Sno='" + StuID + "'"; SqlCommand sqlCommand = new SqlCommand(select_by_id, con); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = sqlDataReader; dataGridView1.DataSource = bindingSource; } catch { MessageBox.Show("查询语句有误,请认真检查SQL语句!"); } finally { con.Close(); } } } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算