C#數(shù)據(jù)庫(kù)操作的用法
由于最近和數(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ù)操作的用法。
- Asp.net(C#)讀取數(shù)據(jù)庫(kù)并生成JS文件制作首頁(yè)圖片切換效果(附demo源碼下載)
- C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫(kù)建模的基本教程
- C#實(shí)例代碼之抽獎(jiǎng)升級(jí)版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù),抽獎(jiǎng)設(shè)置,補(bǔ)抽
- C#創(chuàng)建數(shù)據(jù)庫(kù)及導(dǎo)入sql腳本的方法
- C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫(kù)實(shí)例詳解
- C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法
- C#連接數(shù)據(jù)庫(kù)和更新數(shù)據(jù)庫(kù)的方法
- c#操作附加數(shù)據(jù)庫(kù)的方法
- C#程序連接數(shù)據(jù)庫(kù)及讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié)
相關(guān)文章
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-06asp.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-08C#開(kāi)發(fā)Windows UWP系列之3D變換
這篇文章介紹了C#開(kāi)發(fā)Windows UWP系列之3D變換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序
這篇文章主要介紹了c#生成站點(diǎn)地圖(SiteMapPath)文件的示例,大家參考使用2013-11-11C# winform點(diǎn)擊生成二維碼實(shí)例代碼
這篇文章主要介紹了 C# winform點(diǎn)擊生成二維碼實(shí)例代碼,需要的朋友可以參考下2017-04-04C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理
本文介紹C#利用Newtonsoft接收前端的JSON字符串,并解析反序列化成字典集合,對(duì)其進(jìn)行處理。2016-04-04C#中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