C#訪問SQLServer增刪改查代碼實(shí)例
一個(gè)專門實(shí)現(xiàn)訪問sql server數(shù)據(jù)庫增刪改查的操作代碼,分享給大家,具體內(nèi)容如下
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";//定義數(shù)據(jù)庫連接參數(shù)
SqlConnection MyConnection = new SqlConnection(MyConn);//定義一個(gè)數(shù)據(jù)連接實(shí)例
SqlCommand MyCommand = new SqlCommand("SELECT * FROM 圖書借閱", MyConnection); //定義一個(gè)數(shù)據(jù)庫操作指令
SqlDataAdapter SelectAdapter = new SqlDataAdapter();//定義一個(gè)數(shù)據(jù)適配器
SelectAdapter.SelectCommand = MyCommand;//定義數(shù)據(jù)適配器的操作指令
DataSet MyDataSet = new DataSet();//定義一個(gè)數(shù)據(jù)集
MyConnection.Open();//打開數(shù)據(jù)庫連接
SelectAdapter.SelectCommand.ExecuteNonQuery();//執(zhí)行數(shù)據(jù)庫查詢指令
MyConnection.Close();//關(guān)閉數(shù)據(jù)庫
SelectAdapter.Fill(MyDataSet);//填充數(shù)據(jù)集
DataGrid1.DataSource = MyDataSet.Tables[0];
//DataGrid1.DataBind();//將數(shù)據(jù)表格用數(shù)據(jù)集中的數(shù)據(jù)填充
}
//添加
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 圖書借閱 (圖書編號,讀者編號,續(xù)借次數(shù)) 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);
}
}
}
}
數(shù)據(jù)庫如下;

winform中查詢成功;

插入時(shí),因?yàn)榻栝喚幪枮樽栽?,不能插入值,會自己生成?/p>


更新,外鍵沖突;插入的圖書編號為000999,無此圖書,故出錯(cuò);

插入成功;

更新操作員為"王老師";

刪除借閱編號為31的記錄;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用linq對數(shù)組進(jìn)行篩選排序的方法
這篇文章主要介紹了C#使用linq對數(shù)組進(jìn)行篩選排序的方法,實(shí)例分析了C#實(shí)用linq擴(kuò)展進(jìn)行數(shù)組排序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)將網(wǎng)頁保存成圖片的網(wǎng)頁拍照功能
這篇文章主要介紹了C#實(shí)現(xiàn)將網(wǎng)頁保存成圖片的網(wǎng)頁拍照功能,很實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-07-07
動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果
webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個(gè)webservice暴露在外面可供使用的接口。今天我們來詳細(xì)討論下如何動態(tài)調(diào)用以及讀取解析返回結(jié)果2015-06-06
C#實(shí)現(xiàn)窗體間傳遞數(shù)據(jù)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)窗體間傳遞數(shù)據(jù)實(shí)例,需要的朋友可以參考下2014-07-07
C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)
本文主要介紹了C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

