如何使用 sqldataadapter 来检索多个行
以下代码阐明了如何使用 sqldataadapter 对象发出可生成 dataset 或 datatable 的命令。它从 sql server northwind 数据库中检索一组产品类别。
using system.data;
using system.data.sqlclient;
public datatable retrieverowswithdatatable()
{
using ( sqlconnection conn = new sqlconnection(connectionstring) )
{
conn.open();
sqlcommand cmd = new sqlcommand("datretrieveproducts", conn);
cmd.commandtype = commandtype.storedprocedure;
sqldataadapter adapter = new sqldataadapter( cmd );
datatable datatable = new datatable("products");
adapter .fill(datatable);
return datatable;
}
}
使用 sqladapter 生成 dataset 或 datatable
|
1. |
创建一个 sqlcommand 对象以调用该存储过程,并将其与一个 sqlconnection 对象(显示)或连接字符串(不显示)相关联。 |
|
2. |
创建一个新的 sqldataadapter 对象并将其与 sqlcommand 对象相关联。 |
|
3. |
创建一个 datatable(也可以创建一个 dataset)对象。使用构造函数参数来命名 datatable。 |
|
4. |
调用 sqldataadapter 对象的 fill 方法,用检索到的行填充 dataset 或 datatable。 |
如何使用 sqldatareader 来检索多个行
以下代码片段阐明了可检索多个行的 sqldatareader 方法。
using system.io;
using system.data;
using system.data.sqlclient;
public sqldatareader retrieverowswithdatareader()
{
sqlconnection conn = new sqlconnection(
"server=(local);integrated security=sspi;database=northwind");
sqlcommand cmd = new sqlcommand("datretrieveproducts", conn );
cmd.commandtype = commandtype.storedprocedure;
try
{
conn.open();
// generate the reader. commandbehavior.closeconnection causes the
// the connection to be closed when the reader object is closed
return( cmd.executereader( commandbehavior.closeconnection ) );
}
catch
{
conn.close();
throw;
}
}
// display the product list using the console
private void displayproducts()
{
sqldatareader reader = retrieverowswithdatareader();
try
{
while (reader.read())
{
console.writeline("{0} {1} {2}",
reader.getint32(0).tostring(),
reader.getstring(1) );
}
}
finally
{
reader.close(); // also closes the connection due to the
// commandbehavior enum used when generating the reader
}
}
使用 sqldatareader 检索行
|
1. |
创建一个用来执行存储过程的 sqlcommand 对象,并将其与一个 sqlconnection 对象相关联。 |
|
2. |
打开连接。 |
|
3. |
通过调用 sqlcommand 对象的 executereader 方法创建一个 sqldatareader 对象。 |
|
4. |
要从流中读取数据,请调用 sqldatareader 对象的 read 方法来检索行,并使用类型化访问器方法(如 getint32 和 getstring 方法)来检索列值。 |
|
5. |
使用完读取器后,请调用其 close 方法。 |
如何使用 xmlreader 检索多个行
可以使用 sqlcommand 对象来生成 xmlreader 对象,后者可提供对 xml 数据的基于流的只进访问。命令(通常为存储过程)必须产生基于 xml 的结果集,对于 sql server 2000 而言,该结果集通常包含一个带有有效 for xml 子句的 select 语句。以下代码片段阐明了该方法:
public void retrieveanddisplayrowswithxmlreader()
{
using( sqlconnection conn = new sqlconnection(connectionstring) )
{;
sqlcommand cmd = new sqlcommand("datretrieveproductsxml", conn );
cmd.commandtype = commandtype.storedprocedure;
try
{
conn.open();
xmltextreader xreader = (xmltextreader)cmd.executexmlreader();
while ( xreader.read() )
{
if ( xreader.name == "products" )
{
string stroutput = xreader.getattribute("productid");
stroutput += " ";
stroutput += xreader.getattribute("productname");
console.writeline( stroutput );
}
}
xreader.close(); // xmltextreader does not support idisposable so it cant be
// used within a using keyword
}
}
上述代码使用了以下存储过程:
create procedure datretrieveproductsxml as select * from products for xml auto go
使用 xmlreader 检索 xml 数据
|
1. |
创建一个 sqlcommand 对象来调用可生成 xml 结果集的存储过程(例如,在 select 语句中使用 for xml 子句)。将该 sqlcommand 对象与某个连接相关联。 |
|
2. |
调用 sqlcommand 对象的 executexmlreader 方法,并且将结果分配给只进 xmltextreader 对象。当您不需要对返回的数据进行任何基于 xml 的验证时,这是应该使用的最快类型的 xmlreader 对象。 |
|
3. |
使用 xmltextreader 对象的 read 方法来读取数据。 |
如何使用存储过程输出参数来检索单个行
借助于命名的输出参数,可以调用在单个行内返回检索到的数据项的存储过程。以下代码片段使用存储过程来检索 northwind 数据库的 products 表中包含的特定产品的产品名称和单价。
void getproductdetails( int productid,
out string productname, out decimal unitprice )
{
using( sqlconnection conn = new sqlconnection(
"server=(local);integrated security=sspi;database=northwind") )
{
// set up the command object used to execute the stored proc
sqlcommand cmd = new sqlcommand( "datgetproductdetailsspoutput", conn )
cmd.commandtype = commandtype.storedprocedure;
// establish stored proc parameters.
// @productid int input
// @productname nvarchar(40) output
// @unitprice money output
// must explicitly set the direction of output parameters
sqlparameter paramprodid =
cmd.parameters.add( "@productid", productid );
paramprodid.direction = parameterdirection.input;
sqlparameter paramprodname =
cmd.parameters.add( "@productname", sqldbtype.varchar, 40 );
paramprodname.direction = parameterdirection.output;
sqlparameter paramunitprice =
cmd.parameters.add( "@unitprice", sqldbtype.money );
paramunitprice.direction = parameterdirection.output;
conn.open();
// use executenonquery to run the command.
// although no rows are returned any mapped output parameters
// (and potentially return values) are populated
cmd.executenonquery( );
// return output parameters from stored proc
productname = paramprodname.value.tostring();
unitprice = (decimal)paramunitprice.value;
}
}
使用存储过程输出参数来检索单个行
|
1. |
创建一个 sqlcommand 对象并将其与一个 sqlconnection 对象相关联。 |
|
2. |
通过调用 sqlcommand 的 parameters 集合的 add 方法来设置存储过程参数。默认情况下,参数都被假设为输入参数,因此必须显式设置任何输出参数的方向。 注 一种良好的习惯做法是显式设置所有参数(包括输入参数)的方向。 |
|
3. |
打开连接。 |
|
4. |
调用 sqlcommand 对象的 executenonquery 方法。这将填充输出参数(并可能填充返回值)。 |
|
5. |
通过使用 value 属性,从适当的 sqlparameter 对象中检索输出参数。 |
|
6. |
关闭连接。 |
上述代码片段调用了以下存储过程。
create procedure datgetproductdetailsspoutput
@productid int,
@productname nvarchar(40) output,
@unitprice money output
as
select @productname = productname,
@unitprice = unitprice
from products
where productid = @productid
go
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


