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