Asp tutorial. net dataset, able, and dateview
Ataset is a temporary small warehouse. Through sqldataapert, it can be equivalent to a truck, and data is stored in dataset through the adapter sqldataapert in the database tutorial. Even if the connection is disconnected, it can still deal with the database, the following is an example:
Public static dataset query (string sqlstr)
{
Using (oledbconnection conn = new oledbconnection (connstr ))
{
Try
{
Dataset ds = new dataset ()
Oledbdataadapter da = new oledbdataadapter ();
Da. fill (ds, "ds ");
Return ds;
}
Catch (oledbexception e)
{
Throw new exception (e. message );
}
}
}
We can see the basic usage. We can use the fill method of oledbdataadapter to fill the data in dataset.
Da. fill (ds, "ds"); the ds below indicates that because dataset contains many tables, we actually add the data to the dataset table, if no data is written, the index starts with 0.
Datatable dt = ds. tables ["ds"]
Datatable dt = ds. tables [0];
Next, we need to look at the components of dataset.
Dataset has many able and datatable has many dataview
Datatable consists of many datarow and datacolumn
The specific value is:
Ds. tables [""]. rows [0] ["column name"]
Dataview is the displayed view. For example, if we want to bind it to the girlview data source, dataview is used for display.
Dataview dv = new dataview ();
Dv. table = ds. tables [""]
This. girlview. datasource = dv;
Or use defaultview so that dataview does not need to be instantiated;
This. girlview. datasource = ds. tables [0]. defaultview;
We can also filter dataview.
Ds. tables [0]. defaultview. rowfilter = "id = 1 ";
Or
Dataview dv = new dataview ();
Dv. rowfilter = "id = 1 ";
Dataset multi-Table query
Dataset can be used for multi-table queries or when the data volume is large. The following describes how to perform multi-table queries using dataset. The following is an example.
Public static dataset query (string sqlstring)
{
Using (sqlconnection connection = new sqlconnection (connectionstring ))
{
Dataset ds = new dataset ();
Try
{
Connection. open ();
Sqldataadapter command = new sqldataadapter (sqlstring, connection );
Command. fill (ds, "ds ");
}
Catch (system. data. sqlclient. sqlexception ex)
{
Throw new exception (ex. message );
}
Return ds;
}
}
String citysql = "select * from soncity; select * from partytype ";
Ds = dbhelpersql. query (citysql );
S1.datasource = ds. tables [0]. defaultview;
S1.datatextfield = "sonname ";
S1.databind ();
S2.datasource = ds. tables [1]. defaultview;
S2.datatextfield = "partytytypename ";
S2.databind ();
From the above, we can see how to perform multi-table queries. You can write multiple SQL statements and separate them with semicolons.
String citysql = "select * from soncity; select * from partytype ";
The returned table is returned starting from 0 according to the index.
S1.datasource = ds. tables [0]. defaultview;
S2.datasource = ds. tables [1]. defaultview;
Relationship between dataset and sqlcommand
I don't know if I have thought about this problem when I use sqldataadapter. Why can I connect a database without opening it? Here I will tell you why:
In fact, database connections are implicitly opened.
Sqldataadapter sqldateadapter = new sqldataadapter (SQL statement, connection );
In fact, it implicitly replaces the following code:
Conn. open ();
Sqlcommand cmd = new sqlcommand ();
Cmd. commandtext = "select * from googs ";
Cmd. commandtype = commandtype. text;
Cmd. connection = conn;
Sqldataadapter sqldateadapter = new sqldataadapter ();
Sqldateadapter. selectcommand = cmd;