資源描述:
《c#進(jìn)行sql server數(shù)據(jù)庫(kù)增刪查改操作》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、.C#連接SQLServer數(shù)據(jù)庫(kù)并進(jìn)行增刪查改操作詳細(xì)代碼在下邊,直接可以使用設(shè)計(jì)好的C#界面:...共3個(gè)窗體。數(shù)據(jù)庫(kù):運(yùn)行后的界面:...............代碼:(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;i
4、or.White;this.dataGridView1.Columns[i].DefaultCellStyle.SelectionForeColor=Color.Black;this.dataGridView1.Columns[i].ReadOnly=true;}conn.Close();}catch(Exceptionee){MessageBox.Show("錯(cuò)誤:"+ee.Message,"錯(cuò)誤");}}privatevoidbutton2_Click(objectsender,EventArgse){if(this.dataGridView1.Sele
5、ctedCells==null){MessageBox.Show("請(qǐng)選擇要?jiǎng)h|除的項(xiàng)!","錯(cuò)誤",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("請(qǐng)輸入要查詢的姓名!","提示");}else{SqlConnectionconn=newSqlConnection();stringstr="DataSource=ZWEIQUAN-PC;InitialCatalog=Student;IntegratedSecurity=True";conn.ConnectionString=str;conn.Open();SqlCommandcmd=newSqlCommand(
8、);cmd.CommandText="select*