c#

位置:IT落伍者 >> c# >> 浏览文章

C#数据库操作的三种经典用法


发布日期:2022年11月01日
 
C#数据库操作的三种经典用法

由于最近和数据库打交道需要用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 ddprivate string sqlConnectionCommand = Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd;//This table contains two columns:KeywordID int not nullKeywordName varchar() not nullprivate 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 operateprivate 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 themint 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 databaseSqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);sqlDataAdapterFill(dataSet dataTableName);//Do something to dataset then you can update it to DatabaseHere I just add a rowDataRow row = dataSetTables[]NewRow();row[] = ;row[] = new row;dataSetTables[]RowsAdd(row);sqlDataAdapterUpdate(dataSet dataTableName);sqlCommandDispose();sqlDataAdapterDispose();sqlConnectionClose();}}}

以上的程序概括了最典型的用法也是最基本的用法更多的用法我将会陆续给出大家有什么疑问或建议欢迎来信(j)或留言

               

上一篇:C#:把其他进制的数转换为十进制

下一篇:C#基础应用--Extension