Coding implementation> Microsoft Data Access Technology ADO. Net> use dataset> learn more about Dataset
Dataset is a memory database. dataset uses XML to represent data, not only copies of data in the database, but also imports records from XML and CSV files.
Hierarchical relationship diagram of Dataset
Coding> Microsoft Data Access Technology ADO. Net> dataset> datatable for memory tables
Use datatable to implement memory tables Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;
Namespace memorytable
{
Class Program
{
Static void main (string [] ARGs)
{
// Specify a table name for datatable and access the table name through DT. tablename.
Datatable dt = new datatable ("memory table example ");
// Add schema information for the datatable, that is, create fields.
DT. Columns. Add (New datacolumn ("title", typeof (string )));
DT. Columns. Add (New datacolumn ("book no.", typeof (string )));
Datacolumn [] DCS = new datacolumn [2];
DCS [0] = new datacolumn ("price", typeof (decimal ));
DCS [1] = new datacolumn ("Press", typeof (string ));
DT. Columns. addrange (DCS );
Datarow DR = DT. newrow ();
Dr ["title"] = "C # programming series ";
Dr ["Book Number"] = "12345-678-90 ";
Dr ["price"] = 45.3;
Dr ["Press"] = "my press ";
DT. Rows. Add (DR );
DT. Rows. Add (new object [] {"C # programming Series 2", "33455-333-333", 45.7, "my press "});
Displayresult (DT );
Console. Readline ();
}
Static void displayresult (datatable DT)
{
For (INT I = 0; I <= DT. Columns. Count-1; I ++)
{
Console. Write (Dt. Columns [I]. columnname. padright (10 ));
}
Console. writeline ();
For (INT I = 0; I <= DT. Rows. Count-1; I ++)
{
Foreach (datacolumn Col in DT. columns)
{
Console. Write (Dt. Rows [I] [col]. tostring (). padright (10 ));
}
Console. writeline ();
}
}
}
}
Coding implementation> Microsoft Data Access Technology ADO. Net> use dataset> to fill data in the database into dataset and access data in Dataset
Code Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;
Namespace filldataset
{
Class Program
{
Static void main (string [] ARGs)
{
Const string connectionstr = @ "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ northwnd. MDF; Integrated Security = true; user instance = true ;";
Using (sqlconnection conn = new sqlconnection (connectionstr ))
{
Try
{
Conn. open ();
Dataset DS = new dataset ();
Sqldataadapter daorders = new sqldataadapter ("select Top 2 * from orders", Conn );
Daorders. Fill (DS, "orders ");
Console. writeline ("filling the orders table to dataset succeeded ");
Sqldataadapter daproducts = new sqldataadapter ("select Top 2 * from products", Conn );
Daproducts. Fill (DS, "Products ");
Console. writeline ("successfully filled the Products table to dataset ");
Sqldataadapter daorderdetails = new sqldataadapter ("select Top 2 * from [Order Details]", Conn );
Daorderdetails. Fill (DS, "Order details ");
Console. writeline ("filling the order detail table to dataset succeeded ");
Console. writeline ("the record value in the first column of the First row accessing the orders table is :");
Console. writeline (Ds. Tables [0]. Rows [0] [0]);
// Use the createtablereader method to display all data
Displayresult (DS );
}
Catch (sqlexception ex)
{
Console. writeline ("error occurred during data processing: {0}", Ex. Message );
}
Finally
{
If (conn. State = connectionstate. open)
{
Conn. Close ();
}
}
}
Console. Read ();
}
Static void displayresult (Dataset DS)
{
Using (datatablereader DTR = Ds. createdatareader ())
{
Do
{
If (DTR. hasrows)
{
While (DTR. Read ())
{
For (INT I = 0; I <DTR. fieldcount; I ++)
{
Console. Write (DTR [I] + "");
}
Console. writeline ();
}
}
Else
{
Console. writeline ("no data ");
}
Console. writeline ("---------------------" + environment. newline );
} While (DTR. nextresult ());
}
}
}
}
Dataview objects have more functions than able objects. By using dataview objects, you can sort, search, and select data tables.