DataTable creates rows and columns, and DataReader reads

Source: Internet
Author: User

Public DataTable read ()

{

String str = "select id, UserName, UserPwd from Users"; // SQL statement

SqlCommand cmd = new SqlCommand (str, conn );

DataTable table = new DataTable (); // create a table

DataColumn dc = null; // Definition

Dc = table. Columns. Add ("no.", Type. GetType ("System. Int32"); // create a column, column name, and Type

Dc. AutoIncrement = true; // automatic Column

Dc. AutoIncrementSeed = 1; // start with 1

Dc. AutoIncrementStep = 1; // The step size is 1.

Dc = table. Columns. Add ("User Name", Type. GetType ("System. String"); // create the second column

Dc = table. Columns. Add ("password", Type. GetType ("System. String"); // create the third column


DataRow newRow;

Conn. Open ();

SqlDataReader reader = cmd. ExecuteReader ();

While (reader. Read ())

{

NewRow = table. NewRow (); // create a new row

NewRow ["Number"] = reader ["id"]; // assign a value to the row

NewRow ["username"] = reader ["username"]. ToString ();

NewRow ["password"] = reader ["userpwd"]. ToString ();

Table. Rows. Add (newRow); // Add Rows to the table

}

Return table;

}


This article from the "DotNet learning accumulation" blog, please be sure to keep this source http://00051.blog.51cto.com/4095420/1227068

Related Article

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.