欢迎来到天天文库
浏览记录
ID:31662057
大小:524.00 KB
页数:14页
时间:2019-01-16
《c#进行sql server数据库增删查改操作》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.C#连接SQLServer数据库并进行增删查改操作详细代码在下边,直接可以使用设计好的C#界面:...共3个窗体。数据库:运行后的界面:...............代码:(Form1)publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form1_Load(sender,e);}privatevoidForm1_Load(objectsender,EventArgse){try
2、{SqlConnectionconn=newSqlConnection();...stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="select*from基本信息表";cmd.Connection=conn;SqlDataAdaptersda=newSqlDataAdapter(
3、cmd);DataSetds=newDataSet();sda.Fill(ds,"基本信息表");this.dataGridView1.DataSource=ds;this.dataGridView1.DataMember="基本信息表";this.dataGridView1.AutoGenerateColumns=true;for(inti=1;i4、or.White;this.dataGridView1.Columns[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.Sele5、ctedCells==null){MessageBox.Show("请选择要删¦除的项!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}else{if(this.dataGridView1.CurrentCell.ColumnIndex==0){stringst=this.dataGridView1[1,...this.dataGridView1.CurrentCell.RowIndex].Value.ToString();SqlConnectionconn=newSqlConnection();stri6、ngstr="DataSource=ZWEIQUAN-PC;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(obj7、ectsender,EventArgse){if(this.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand(8、);cmd.CommandText="select*
4、or.White;this.dataGridView1.Columns[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.Sele
5、ctedCells==null){MessageBox.Show("请选择要删¦除的项!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}else{if(this.dataGridView1.CurrentCell.ColumnIndex==0){stringst=this.dataGridView1[1,...this.dataGridView1.CurrentCell.RowIndex].Value.ToString();SqlConnectionconn=newSqlConnection();stri
6、ngstr="DataSource=ZWEIQUAN-PC;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(obj
7、ectsender,EventArgse){if(this.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand(
8、);cmd.CommandText="select*
此文档下载收益归作者所有