SqlAdapter generates DataSet or DataTable

Source: Internet
Author: User

Sqladapter generates dataset or datatable
1. Create a sqlcommand object to call the stored procedure and associate it with a sqlconnection object (displayed) or connection string (not displayed.
 
2. Create a New sqldataadapter object and associate it with the sqlcommand object.
 
3. Create a able (or a dataset) object. Use the constructor parameters to name the able.
 
4. Call the fill method of the sqldataadapter object and fill the dataset or able with the retrieved rows.

 

How to Use sqldatareader to retrieve multiple rows
The following code snippet clarifies the sqldatareader Method for retrieving multiple rows.

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
15 // the connection to be closed when the reader object is closed
16 return (cmd.exe cutereader (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
41 // commandbehavior enum used when generating the reader
42}
43}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.