由於最近和數據庫打交道需要用C#和SQL Server 進行操作就把近段時間內的最常用的操作做個總結本人也是第一次用C#操作數據庫所以這三種典型用法對初學者還是挺有幫助的
以下是我在visual studio 上寫的一個類(連的是SQL Server )已經過測試通過裡面有個方法比較典型源碼如下
using System; using SystemCollectionsGeneric; using SystemText; using SystemData; using SystemDataSqlClient; namespace DatabaseOperate { class SqlOperateInfo { //Suppose your ServerName is aaDatabaseName is bbUserName 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 nullKeywordName varchar() 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 = ; public void UseSqlReader() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommandCommandType = SystemDataCommandTypeText; sqlCommandConnection = sqlConnection; sqlCommandCommandText = sqlSelectCommand; sqlConnectionOpen(); SqlDataReader sqlDataReader = sqlCommandExecuteReader(); while(sqlDataReaderRead()) { //Get KeywordID and KeywordName You can do anything you like Here I just output them int keywordid = (int)sqlDataReader[]; //the same as: int keywordid = (int)sqlDataReader[KeywordID] string keywordName = (string)sqlDataReader[]; //the same as: string keywordName = (int)sqlDataReader[KeywordName] ConsoleWriteLine(KeywordID = + keywordid + KeywordName = + keywordName); } sqlDataReaderClose(); sqlCommandDispose(); sqlConnectionClose(); } public void UseSqlStoredProcedure() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommandCommandType = CommandTypeStoredProcedure; sqlCommandConnection = sqlConnection; sqlCommandCommandText = storedProcedureName; sqlConnectionOpen(); sqlCommandExecuteNonQuery(); //you can use reader heretooas long as you modify the sp and let it like select * from sqlCommandDispose(); sqlConnectionClose(); } public void UseSqlDataSet() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommandCommandType = SystemDataCommandTypeText; sqlCommandConnection = sqlConnection; sqlCommandCommandText = sqlSelectCommand; sqlConnectionOpen(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapterSelectCommand = sqlCommand; DataSet dataSet = new DataSet(); //sqlCommandBuilder is for update the dataset to database SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlDataAdapterFill(dataSet dataTableName); //Do something to dataset then you can update it to DatabaseHere I just add a row DataRow row = dataSetTables[]NewRow(); row[] = ; row[] = new row; dataSetTables[]RowsAdd(row); sqlDataAdapterUpdate(dataSet dataTableName); sqlCommandDispose(); sqlDataAdapterDispose(); sqlConnectionClose(); } } }
以上的程序概括了最典型的用法也是最基本的用法更多的用法我將會陸續給出大家有什麼疑問或建議歡迎來信(j)或留言