Method 1: Create a DataSet object
Dataset DS = new dataset ();
Dataset DS = new dataset ("datasetname ");
2. Fill dataset with a dataset
The most common method is to fill the data with dataadapter object fill ().
(1)
Dataset DS = new dataset ();
Sqldataadapter Adapt = new sqldataadapter (sqlcmd, con)
Adapt. Fill (DS, "mytest ");
(2)
Dataset DS = new dataset ();
Datatable dt = new datatable ("newtable ");
DS. Tables. Add (DT );
(3)
Dataset DS = new dataset ();
Datatable dt = Ds. Tables. Add ("newtable ");
3. Access the table, row, and column values in dataset (1): access each datatable
Access by Table Name: Ds. Tables ["mytest"] // specify the datatable object mytest (that is, access the datatable named mytest in dataset)
Access by index (index based on 0): Ds. Tables [0] // specify the first able in Dataset
(2): Access the rows in the datatable
DS. Tables ["mytest"]. Rows [N] // access row n + 1 of the mytest table (the row index starts from 0)
DS. Tables [I]. Rows [N] // access the n + 1 column of the I + 1 able in dataset (the column index starts from 0)
(3): Access an element in the datatable
DS. Tables ["mytest"]. Rows [N] [m] // access the elements in column M + 1 of row n + 1 of mytest.
DS. Tables [I]. Rows [N] [m] // access the elements in column M + 1 of row n + 1 of the I + 1 able table in Dataset
DS. Tables ["mytest"]. Rows [N] [name] // access the element of the name column in the n + 1 row of the mytest table
DS. Tables [I]. Rows [N] [name] // access the elements in the name column of the n + 1 row of the I + 1 able table in Dataset
(4): retrieve the column name in the datatable
DS. Tables ["mytest"]. Columns [N] // retrieve the n + 1 column name of the mytest table
DS. Tables [I]. Columns [N]
4. Instances
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data. sqlclient;
Using system. Data;
Namespace sqlconnection1
{
Class Program
{
Private void sqlconnectionf (string source, string select)
{
// Create a connection
Sqlconnection con = new sqlconnection (source );
Sqldataadapter Adapt = new sqldataadapter (select, con );
Try
{
Con. open ();
Console. writeline ("connection is successful! ");
}
Catch (exception E)
{
Console. writeline ("connection error is: {0}", E. tostring ());
}
// Create a dataset
Dataset DS = new dataset ();
// Add data to Dataset
Adapt. Fill (DS, "mytest ");
// Retrieve the names of columns in the mytest table
Console. writeline ("{0,-15} {1,-10} {2,-10}", DS. Tables ["mytest"]. Columns [0],
DS. Tables ["mytest"]. Columns [1], DS. Tables ["mytest"]. Columns [2]);
// Output the sixth row of the mytest table
Datarow row1 = Ds. Tables ["mytest"]. Rows [5];
Console. writeline ("{0,-15} {1,-10} {2,-10}", row1 [0], row1 [1], row1 [2]);
// Output the value of the second column of the fifth row in the mytest table
Datarow row2 = Ds. Tables ["mytest"]. Rows [4];
Console. writeline ("{0,-25}", row2 [1]);
// The following two methods are equivalent to row2 [1] (that is, the value of the second column of the fifth line)
Console. writeline ("{0,-25}", DS. Tables ["mytest"]. Rows [4] [1]);
Console. writeline ("{0,-25}", DS. Tables ["mytest"]. Rows [4] ["Number"]);
// Output all data in Dataset
Foreach (datarow row in DS. Tables ["mytest"]. Rows)
{
Console. writeline ("{0,-15} {1,-10} {2,-10} {3}", row ["name"],
Row ["Number"], row ["low"], row ["high"]);
// Obtain the value of the third column
Console. writeline ("{0,-15}", row [3]);
}
Console. Readline ();
Con. Close ();
}
Static void main (string [] ARGs)
{
String sou = "Server = duanyf // sqlexpress;" + "Initial catalog = Master;" + "uid = sa;" + "Password = dyf123 ";
String sel = "Select name, number, low, high from DBO. spt_values ";
Program sqlcon = new program ();
Sqlcon. sqlconnectionf (SOU, Sel );
}
}
}