欢迎来到天天文库
浏览记录
ID:47426683
大小:538.50 KB
页数:14页
时间:2020-01-10
《C#连接数据库及增删查改操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C#连接数据库及增删查改操作设计好的C#界面:共3个窗体。数据库:运行后的界面:代码:(Form1)publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form1_Load(sender,e);}privatevoidForm1_Load(objectsender,EventArgse){try{SqlConnectionconn=newSqlConnection();st
2、ringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="select*from基本信息表";cmd.Connection=conn;SqlDataAdaptersda=newSqlDataAdapter(cmd);DataSetds=newDataSet();sda.Fill(ds
3、,"基本信息表");this.dataGridView1.DataSource=ds;this.dataGridView1.DataMember="基本信息表";this.dataGridView1.AutoGenerateColumns=true;for(inti=1;i4、[i].DefaultCellStyle.SelectionForeColor=Color.Black;this.dataGridView1.Columns[i].ReadOnly=true;}conn.Close();}catch(Exceptionee){MessageBox.Show("错误:"+ee.Message,"错误");}}privatevoidbutton2_Click(objectsender,EventArgse){if(this.dataGridView1.SelectedCells==null){MessageBox.Sho5、w("请选择要删¦除的项!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}else{if(this.dataGridView1.CurrentCell.ColumnIndex==0){stringst=this.dataGridView1[1,this.dataGridView1.CurrentCell.RowIndex].Value.ToString();SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;6、InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="deletefrom基本信息表where姓名='"+st+"'";cmd.Connection=conn;cmd.ExecuteNonQuery();conn.Close();}}}privatevoidbutton3_Click(objectsender,EventArgse){if(t7、his.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="sel8、ect*from基本信息表where姓名='"+this.textBox1.Text.Trim()+"'";
4、[i].DefaultCellStyle.SelectionForeColor=Color.Black;this.dataGridView1.Columns[i].ReadOnly=true;}conn.Close();}catch(Exceptionee){MessageBox.Show("错误:"+ee.Message,"错误");}}privatevoidbutton2_Click(objectsender,EventArgse){if(this.dataGridView1.SelectedCells==null){MessageBox.Sho
5、w("请选择要删¦除的项!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}else{if(this.dataGridView1.CurrentCell.ColumnIndex==0){stringst=this.dataGridView1[1,this.dataGridView1.CurrentCell.RowIndex].Value.ToString();SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;
6、InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="deletefrom基本信息表where姓名='"+st+"'";cmd.Connection=conn;cmd.ExecuteNonQuery();conn.Close();}}}privatevoidbutton3_Click(objectsender,EventArgse){if(t
7、his.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="sel
8、ect*from基本信息表where姓名='"+this.textBox1.Text.Trim()+"'";
此文档下载收益归作者所有