欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#数据库操作的用法

程序员文章站 2023-02-21 15:34:57
由于最近和数据库打交道,需要用c#和sql server 2005进行操作,就把近段时间内的最常用的操作做个总结。本人也是第一次用c#操作数据库,所以这三种典型用法对初学者...

由于最近和数据库打交道,需要用c#和sql server 2005进行操作,就把近段时间内的最常用的操作做个总结。本人也是第一次用c#操作数据库,所以这三种典型用法对初学者还是挺有帮助的。

以下是我在visual studio 2005上写的一个类(连的是sql server 2005),已经过测试通过。里面有3个方法比较典型,源码如下:

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

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

希望通过本文的介绍,能给你带来帮助,学会c#数据库操作的用法。