欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#數(shù)據(jù)庫(kù)操作的用法

 更新時(shí)間:2015年10月15日 14:12:13   投稿:lijiao  
這篇文章主要介紹了C#數(shù)據(jù)庫(kù)操作的三種經(jīng)典用法

由于最近和數(shù)據(jù)庫(kù)打交道,需要用C#和SQL Server 2005進(jìn)行操作,就把近段時(shí)間內(nèi)的最常用的操作做個(gè)總結(jié)。本人也是第一次用C#操作數(shù)據(jù)庫(kù),所以這三種典型用法對(duì)初學(xué)者還是挺有幫助的。

以下是我在visual studio 2005上寫(xiě)的一個(gè)類(lèi)(連的是SQL Server 2005),已經(jīng)過(guò)測(cè)試通過(guò)。里面有3個(gè)方法比較典型,源碼如下:

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(); 
} 
} 
} 

以上的程序概括了最典型的用法,也是最基本的用法。

希望通過(guò)本文的介紹,能給你帶來(lái)幫助,學(xué)會(huì)C#數(shù)據(jù)庫(kù)操作的用法。

相關(guān)文章

  • C#多線程系列之進(jìn)程同步Mutex類(lèi)

    C#多線程系列之進(jìn)程同步Mutex類(lèi)

    本文詳細(xì)講解了C#多線程中的進(jìn)程同步Mutex類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#中Abstract 、Virtual和Override的使用及區(qū)別

    C#中Abstract 、Virtual和Override的使用及區(qū)別

    C#中virtual,abstract,override用于方法重載,子類(lèi)覆蓋了父類(lèi)的相同方法,父類(lèi)中的實(shí)現(xiàn)不可能再被外面調(diào)用。本文給大家重點(diǎn)介紹C#中Abstract 、Virtual和Override的使用及區(qū)別,需要的朋友參考下吧
    2021-06-06
  • c# 獲得本地ip地址的三種方法

    c# 獲得本地ip地址的三種方法

    這篇文章主要介紹了c# 獲得本地ip地址的三種方法,幫助大家更好的理解和實(shí)用c#,感興趣的朋友可以了解下
    2020-12-12
  • asp.net中調(diào)用oracle存儲(chǔ)過(guò)程的方法

    asp.net中調(diào)用oracle存儲(chǔ)過(guò)程的方法

    存儲(chǔ)過(guò)程是在大型數(shù)據(jù)庫(kù)系統(tǒng)中,一組為了完成特定功能的SQL 語(yǔ)句集,存儲(chǔ)在數(shù)據(jù)庫(kù)中經(jīng)過(guò)第一次編譯后再次調(diào)用不需要再次編譯,用戶通過(guò)指定存儲(chǔ)過(guò)程的名字并給出參數(shù)來(lái)執(zhí)行它,下面給大家介紹下asp.net中調(diào)用oracle存儲(chǔ)過(guò)程的方法,需要的朋友可以參考下
    2015-08-08
  • C#開(kāi)發(fā)Windows UWP系列之3D變換

    C#開(kāi)發(fā)Windows UWP系列之3D變換

    這篇文章介紹了C#開(kāi)發(fā)Windows UWP系列之3D變換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序

    c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序

    這篇文章主要介紹了c#生成站點(diǎn)地圖(SiteMapPath)文件的示例,大家參考使用
    2013-11-11
  • C# winform點(diǎn)擊生成二維碼實(shí)例代碼

    C# winform點(diǎn)擊生成二維碼實(shí)例代碼

    這篇文章主要介紹了 C# winform點(diǎn)擊生成二維碼實(shí)例代碼,需要的朋友可以參考下
    2017-04-04
  • C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理

    C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理

    本文介紹C#利用Newtonsoft接收前端的JSON字符串,并解析反序列化成字典集合,對(duì)其進(jìn)行處理。
    2016-04-04
  • c#的params參數(shù)使用示例

    c#的params參數(shù)使用示例

    這篇文章主要介紹了c#的params參數(shù)使用示例,需要的朋友可以參考下
    2014-04-04
  • C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    這篇文章主要給大家介紹了關(guān)于C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09

最新評(píng)論