C#數(shù)據(jù)庫操作的用法
由于最近和數(shù)據(jù)庫打交道,需要用C#和SQL Server 2005進(jìn)行操作,就把近段時(shí)間內(nèi)的最常用的操作做個總結(jié)。本人也是第一次用C#操作數(shù)據(jù)庫,所以這三種典型用法對初學(xué)者還是挺有幫助的。
以下是我在visual studio 2005上寫的一個類(連的是SQL Server 2005),已經(jīng)過測試通過。里面有3個方法比較典型,源碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseOperate
{
class SqlOperateInfo
{
//Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd"
private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd";
//This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null
private string dataTableName = "Basic_Keyword_Test";
private string storedProcedureName = "Sp_InertToBasic_Keyword_Test";
private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test";
//sqlUpdateCommand could contain "insert" , "delete" , "update" operate
private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1";
public void UseSqlReader()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sqlSelectCommand;
sqlConnection.Open();
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
while(sqlDataReader.Read())
{
//Get KeywordID and KeywordName , You can do anything you like. Here I just output them.
int keywordid = (int)sqlDataReader[0];
//the same as: int keywordid = (int)sqlDataReader["KeywordID"]
string keywordName = (string)sqlDataReader[1];
//the same as: string keywordName = (int)sqlDataReader["KeywordName"]
Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName);
}
sqlDataReader.Close();
sqlCommand.Dispose();
sqlConnection.Close();
}
public void UseSqlStoredProcedure()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = storedProcedureName;
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
//you can use reader here,too.as long as you modify the sp and let it like select * from ....
sqlCommand.Dispose();
sqlConnection.Close();
}
public void UseSqlDataSet()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sqlSelectCommand;
sqlConnection.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataSet dataSet = new DataSet();
//sqlCommandBuilder is for update the dataset to database
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.Fill(dataSet, dataTableName);
//Do something to dataset then you can update it to Database.Here I just add a row
DataRow row = dataSet.Tables[0].NewRow();
row[0] = 10000;
row[1] = "new row";
dataSet.Tables[0].Rows.Add(row);
sqlDataAdapter.Update(dataSet, dataTableName);
sqlCommand.Dispose();
sqlDataAdapter.Dispose();
sqlConnection.Close();
}
}
}
以上的程序概括了最典型的用法,也是最基本的用法。
希望通過本文的介紹,能給你帶來幫助,學(xué)會C#數(shù)據(jù)庫操作的用法。
- Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)
- C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫建模的基本教程
- C#實(shí)例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補(bǔ)抽
- C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法
- C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫實(shí)例詳解
- C#操作數(shù)據(jù)庫中存取圖片文件的方法
- C#連接數(shù)據(jù)庫和更新數(shù)據(jù)庫的方法
- c#操作附加數(shù)據(jù)庫的方法
- C#程序連接數(shù)據(jù)庫及讀取數(shù)據(jù)庫中字段的簡單方法總結(jié)
相關(guān)文章
WPF中下拉框可作選擇項(xiàng)也可以作為只讀文本框使用的方法
這篇文章主要給大家介紹了關(guān)于WPF中下拉框可以選擇項(xiàng)也可以作為只讀文本框使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
C#實(shí)現(xiàn)獲取不同對象中名稱相同屬性的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取不同對象中名稱相同屬性的方法,涉及C#操作類與對象屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
淺談C# 非模式窗體show()和模式窗體showdialog()的區(qū)別
下面小編就為大家?guī)硪黄獪\談C# 非模式窗體show()和模式窗體showdialog()的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07
基于C#實(shí)現(xiàn)PDF按頁分割文件和分頁合并
iTextSharp 是一個開源的 PDF 處理庫,用于在 C# 程序中創(chuàng)建、編輯和處理 PDF 文件,本文將使用iTextSharp實(shí)現(xiàn)C# PDF分割與合并,感興趣的可以了解下2025-03-03
C#中控件動態(tài)添加事件綁定的時(shí)機(jī)詳解
這篇文章主要給大家介紹了在C#中為控件動態(tài)添加事件綁定的時(shí)機(jī)的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C# 4.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

