【www.gdgbn.com--php函数】

sqladapter 生成 dataset 或 datatable
1. 创建一个 sqlcommand 对象以调用该存储过程,并将其与一个 sqlconnection 对象(显示)或连接字符串(不显示)相关联。
 
2. 创建一个新的 sqldataadapter 对象并将其与 sqlcommand 对象相关联。
 
3. 创建一个 datatable(也可以创建一个 dataset)对象。使用构造函数参数来命名 datatable。
 
4. 调用 sqldataadapter 对象的 fill 方法,用检索到的行填充 dataset 或 datatable。

 

如何使用 sqldatareader 来检索多个行
以下代码片段阐明了可检索多个行的 sqldatareader 方法

using system.io;
2 using system.data;
3 using system.data.sqlclient;
4
5 public sqldatareader retrieverowswithdatareader()
6 {
7   sqlconnection conn = new sqlconnection(
8          "server=(local);integrated security=sspi;database=northwind");
9   sqlcommand cmd = new sqlcommand("datretrieveproducts", conn );
10   cmd.commandtype = commandtype.storedprocedure;
11  try
12   {
13     conn.open();
14     // generate the reader. commandbehavior.closeconnection causes the
15     // the connection to be closed when the reader object is closed
16     return( cmd.executereader( commandbehavior.closeconnection ) );
17   }
18  catch
19   {
20     conn.close();
21     throw;
22   }
23 }
24
25 // display the product list using the console
26 private void displayproducts()
27 {
28   sqldatareader reader = retrieverowswithdatareader();
29  try
30   {
31     while (reader.read())
32     {
33       console.writeline("{0} {1} {2}",
34                         reader.getint32(0).tostring(),
35                         reader.getstring(1) );
36     }
37   }
38  finally
39   {
40     reader.close(); // also closes the connection due to the
41                     // commandbehavior enum used when generating the reader
42   }
43 }

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