国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#訪問SQLServer增刪改查代碼實例

2020-01-24 01:00:33
字體:
來源:轉載
供稿:網友

一個專門實現訪問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的記錄;


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 墨江| 杭锦后旗| 胶南市| 曲周县| 苗栗市| 古丈县| 商洛市| 舞钢市| 泌阳县| 营山县| 内江市| 正定县| 宜丰县| 石城县| 凉城县| 基隆市| 横峰县| 二手房| 武汉市| 峨山| 宜君县| 安康市| 新民市| 宜君县| 湘阴县| 鱼台县| 兖州市| 新蔡县| 高雄县| 呼图壁县| 榆树市| 淮滨县| 洛阳市| 柘城县| 阿拉善盟| 衡东县| 鲜城| 铜山县| 大英县| 司法| 揭东县|