C#数据库操作的3种典型用法

C#数据库操作的3种典型用法,第1张

C#数据库操作的3种典型用法,第2张

最近需要用C#和SQL Server 2005操作数据库,所以我就总结一下最近一段时间最常用的操作。我是第一次用C#操作数据库,所以这三个典型用法对新手还是挺有帮助的。

下面是我用visual studio 2005(用SQL Server 2005)写的一个类,已经测试通过。里面有三种典型的方法,所以把源代码贴在这里:
使用System
使用系统。集合。泛型;
使用系统。文本;
使用系统。数据;
使用系统。Data . SqlClient

namespace database operate
{
class SQL operation info
{
//假设你的服务器名是“aa”,数据库名是“bb”,用户名是“cc”,密码是“DD”
私有字符串sqlConnectionCommand = " Data Source = aa;初始目录= bb用户ID = ccpwd = DD ";
//此表包含两列:KeywordID int not null,Keyword name varchar(100)not null
私有字符串dataTableName = " Basic _ Keyword _ Test ";

私有字符串storedProcedureName = " Sp _ inert tobasic _ Keyword _ Test ";
private string sqlSelectCommand = " Select Keyword id,Keyword name From Basic _ Keyword _ Test ";
//sqlUpdateCommand可以包含“插入”、“删除”、“更新”操作
私有字符串sqlUpdateCommand = " Delete From Basic _ Keyword _ Test Where Keyword id = 1 ";

public void use SQL reader()
{
SqlConnection SqlConnection = new SqlConnection(SqlConnection command);
SqlCommand SqlCommand = new SqlCommand();
sqlCommand。CommandType = System。Data . CommandType.Text
sqlCommand。连接= sqlConnection
sqlCommand。CommandText = sqlSelectCommand

sqlConnection。open();
sqldata reader sqldata reader = sqlCommand。ExecuteReader();
while(sqlDataReader。read())
{
//获取关键字ID和关键字名称,你可以做任何你喜欢的事情。这里我只是输出它们。
int keyword id =(int)sqlDataReader[0];
//同:int keyword id =(int)sqldata reader[" keyword id "]
string keyword name =(string)sqldata reader[1];
//same as:string keyword name =(int)sqlDataReader[" keyword name "]
Console。WriteLine(" keyword id = "+keyword id+",keyword name = "+keyword name);
}

sqldata reader。close();
sqlCommand。dispose();
sqlConnection。close();
}
public void UseSqlStoredProcedure()
{
SqlConnection SqlConnection = new SqlConnection(SqlConnection command);
SqlCommand SqlCommand = new SqlCommand();
sqlCommand。CommandType = CommandType。存储过程;
sqlCommand。连接= sqlConnection
sqlCommand。CommandText = storedProcedureName;

sqlConnection。open();
sqlCommand。ExecuteNonQuery();
//您也可以在这里使用reader,只要您修改sp并让它像select * from....

sqlCommand。dispose();
sqlConnection。close();
}
public void UseSqlDataSet()
{
SqlConnection SqlConnection = new SqlConnection(SqlConnection command);
SqlCommand SqlCommand = new SqlCommand();
sqlCommand。CommandType = System。Data . CommandType.Text
sqlCommand。连接= sqlConnection
sqlCommand。CommandText = sqlSelectCommand

sqlConnection。open();
sqldata adapter sqldata adapter = new sqldata adapter();
sqlDataAdapter。SelectCommand = sqlCommand
DataSet数据集=新数据集();
//sqlCommandBuilder用于将数据集更新到数据库
sqlCommandBuilder sqlCommandBuilder = new sqlCommandBuilder(sqldata adapter);
sqlDataAdapter。Fill(dataSet,dataTableName);

//对数据集做些什么,然后可以将它更新到数据库。这里我只是添加了一行
DataRow row = dataSet。表格[0]。NewRow();
row[0]= 10000;
row[1] = "新行";
数据集。tables[0]. rows . add(row);

sqlDataAdapter。Update(dataSet,dataTableName);

sqlCommand。dispose();
sqlDataAdapter。dispose();
sqlConnection。close();
}
}
}

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C#数据库操作的3种典型用法

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情