欢迎来到天天文库
浏览记录
ID:32672490
大小:525.00 KB
页数:14页
时间:2019-02-14
《c#进行sqlserver数据库增删查改操作》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.C#连接SQLServer数据库并进行增删查改操作详细代码在下边,直接可以使用设计好的C#界面:...共3个窗体。数据库:运行后的界面:...............代码:(Form1)publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){Form1_Load(sender,e);}privatevoidForm1_Load(objectsender,EventArgse){try{SqlConnectionco
2、nn=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(cmd);DataSetds=newDataSet();sda.
3、Fill(ds,"基本信息表");this.dataGridView1.DataSource=ds;this.dataGridView1.DataMember="基本信息表";this.dataGridView1.AutoGenerateColumns=true;for(inti=1;i4、llStyle.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.Show("请选择要删¦除的项!","错误",MessageBoxBut5、tons.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;InitialCatalog=Student;IntegratedSecurity=True";co6、nn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="deletefrom基本信息表where姓名='"+st+"'";cmd.Connection=conn;cmd.ExecuteNonQuery();conn.Close();}}}privatevoidbutton3_Click(objectsender,EventArgse){if(this.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else7、{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="select*
4、llStyle.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.Show("请选择要删¦除的项!","错误",MessageBoxBut
5、tons.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;InitialCatalog=Student;IntegratedSecurity=True";co
6、nn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="deletefrom基本信息表where姓名='"+st+"'";cmd.Connection=conn;cmd.ExecuteNonQuery();conn.Close();}}}privatevoidbutton3_Click(objectsender,EventArgse){if(this.textBox1.Text.Trim()==""){MessageBox.Show("请输入要查询的姓名!","提示");}else
7、{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="select*
此文档下载收益归作者所有