【www.gdgbn.com--php应用】

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 表中包含的特定产品的产品名称和单价。  
 1 void getproductdetails( int productid, 
2 out string productname, out decimal unitprice )
3 {
4 using( sqlconnection conn = new sqlconnection(
5 "server=(local);integrated security=sspi;database=northwind") )
6 {
7 // set up the command object used to execute the stored proc
8 sqlcommand cmd = new sqlcommand( "datgetproductdetailsspoutput", conn )
9 cmd.commandtype = commandtype.storedprocedure;
10 // establish stored proc parameters.
11 // @productid int input
12 // @productname nvarchar(40) output
13 // @unitprice money output
14
15 // must explicitly set the direction of output parameters
16 sqlparameter paramprodid =
17 cmd.parameters.add( "@productid", productid );
18 paramprodid.direction = parameterdirection.input;
19 sqlparameter paramprodname =
20 cmd.parameters.add( "@productname", sqldbtype.varchar, 40 );
21 paramprodname.direction = parameterdirection.output;
22 sqlparameter paramunitprice =
23 cmd.parameters.add( "@unitprice", sqldbtype.money );
24 paramunitprice.direction = parameterdirection.output;
25
26 conn.open();
27 // use executenonquery to run the command.
28 // although no rows are returned any mapped output parameters
29 // (and potentially return values) are populated
30 cmd.executenonquery( );
31 // return output parameters from stored proc
32 productname = paramprodname.value.tostring();
33 unitprice = (decimal)paramunitprice.value;
34 }
35 }
使用存储过程输出参数来检索单个行
1. 创建一个 sqlcommand 对象并将其与一个 sqlconnection 对象相关联。
 
2. 通过调用 sqlcommand 的 parameters 集合的 add 方法来设置存储过程参数。默认情况下,参数都被假设为输入参数,因此必须显式设置任何输出参数的方向。 注 一种良好的习惯做法是显式设置所有参数(包括输入参数)的方向。
 
3. 打开连接。
 
4. 调用 sqlcommand 对象的 executenonquery 方法。这将填充输出参数(并可能填充返回值)。
 
5. 通过使用 value 属性,从适当的 sqlparameter 对象中检索输出参数。
 
6. 关闭连接。
  上述代码片段调用了以下存储过程。  
 1 create procedure datgetproductdetailsspoutput
2 @productid int,
3 @productname nvarchar(40) output,
4 @unitprice money output
5 as
6 select @productname = productname,
7 @unitprice = unitprice
8 from products
9 where productid = @productid
10 go

本文来源:http://www.gdgbn.com/jiaocheng/29540/