C#访问SQLServer增删改查代码实例
生活随笔
收集整理的這篇文章主要介紹了
C#访问SQLServer增删改查代码实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
碼碼:
?
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的記錄;
?
設置sql server 自增字段按下圖設置列屬性;
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C#访问SQLServer增删改查代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dNet命令行编译命令CSC使用详细图解
- 下一篇: 图解Oracle用户管理