C#拜訪SQLServer增刪改查代碼實例。本站提示廣大學習愛好者:(C#拜訪SQLServer增刪改查代碼實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#拜訪SQLServer增刪改查代碼實例正文
一個專門完成拜訪sql server數據庫增刪改查的操作代碼,分享給年夜家,詳細內容以下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //查詢 private void button1_Click(object sender, EventArgs e) { string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no";//界說數據庫銜接參數 SqlConnection MyConnection = new SqlConnection(MyConn);//界說一個數據銜接實例 SqlCommand MyCommand = new SqlCommand("SELECT * FROM 圖書借閱", MyConnection); //界說一個數據庫操作指令 SqlDataAdapter SelectAdapter = new SqlDataAdapter();//界說一個數據適配器 SelectAdapter.SelectCommand = MyCommand;//界說數據適配器的操作指令 DataSet MyDataSet = new DataSet();//界說一個數據集 MyConnection.Open();//翻開數據庫銜接 SelectAdapter.SelectCommand.ExecuteNonQuery();//履行數據庫查詢指令 MyConnection.Close();//封閉數據庫 SelectAdapter.Fill(MyDataSet);//填湊數據集 DataGrid1.DataSource = MyDataSet.Tables[0]; //DataGrid1.DataBind();//將數據表格用數據集中的數據填充 } //添加 private void button2_Click(object sender, EventArgs e) { string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no"; SqlConnection MyConnection = new SqlConnection(MyConn); string MyInsert = "insert into 圖書借閱 (圖書編號,讀者編號,續借次數) values ('" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text)+ "','"+Convert.ToInt32(textBox4.Text)+ "')"; SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection); try//異常處置 { MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //更新 private void button3_Click(object sender, EventArgs e) { string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no"; SqlConnection MyConnection = new SqlConnection(MyConn); string MyUpdate = "Update 圖書借閱 set 操作員='" + textBox2.Text + "'" + " where 借閱編號=" + "'" + textBox1.Text + "'"; SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection); try { MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); textBox1.Text = ""; } catch (Exception ex) { MessageBox.Show(ex.Message); } } //刪除 private void button4_Click(object sender, EventArgs e) { string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no"; SqlConnection MyConnection = new SqlConnection(MyConn); string MyDelete = "Delete from 圖書借閱 where 借閱編號=" + textBox1.Text; SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection); try { MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); textBox1.Text = ""; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
數據庫以下;
winform中查詢勝利;
拔出時,由於借閱編號為自增,不克不及拔出值,會本身生成;
更新,外鍵抵觸;拔出的圖書編號為000999,無此圖書,故失足;
拔出勝利;
更新操作員為"王先生";
刪除借閱編號為31的記載;
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。