Asp.net sqldataadapter object usage notes

Source: Internet
Author: User
Tags ibm db2 ibm db2 database

Sqldataadapter
Sqlconnection nwindconn = new sqlconnection ("Data Source = localhost; Integrated Security = sspi; initial catalog = northwind ");
Sqlcommand selectcmd = new sqlcommand ("select customerid, companyName from customers", nwindconn );
Selectcmd. commandtimeout = 30;
Sqldataadapter custda = new sqldataadapter ();
Custda. selectcommand = selectcmd; // use sqlcommand to set parameters for sqldataadapter. You can also use the SELECT statement directly.
Nwindconn. open ();
Dataset custds = new dataset ();
Custda. Fill (custds, "MERs ");
Nwindconn. Close ();
Multiple result sets
If dataadapter encounters multiple result sets, it creates multiple tables in dataset. The incremental default name tablen will be provided to these tables, and "table" indicating table0 is the first table name. If you pass the table name as a parameter to the fill method, the default increasing name tablenamen is provided to these tables. These table names start with "tablename" indicating tablename0.
Fill dataset from multiple dataadapters
You can use any number of dataadapters with a dataset. Each dataadapter can be used to fill in one or more able objects and parse the updates back to the relevant data source. Datarelation and constraint objects can be locally added to dataset, so that you can associate data from multiple different data sources. For example, dataset can contain data from a Microsoft SQL Server database, an IBM DB2 database published through OLE DB, and a data source that processes XML streams. One or more dataadapter objects can communicate with each data source.
Below Code Example: Fill in the customer list from the northwind database on Microsoft SQL Server 2000, from? In Access 2000, the northwind database fills the order list. The filled table is associated with datarelation, and the customer list is displayed with the order of the corresponding customer. For more information about datarelation objects, see add inter-Table relationships and navigation between tables.
Sqlconnection custconn = new sqlconnection ("Data Source = localhost; Integrated Security = sspi; initial catalog = northwind ;");
Sqldataadapter custda = new sqldataadapter ("select * from MERs", custconn );
Oledbconnection orderconn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; "+" Data Source = c: \ Program Files \ Microsoft Office \ samples \ northwind. MDB ;");
Oledbdataadapter orderda = new oledbdataadapter ("select * from orders", orderconn );
Custconn. open ();
Orderconn. open ();
Dataset custds = new dataset ();
Custda. Fill (custds, "MERs ");
Orderda. Fill (custds, "orders ");
Custconn. Close ();
Orderconn. Close ();
Datarelation custorderrel =
Custds. relations. add ("custorders", custds. tables ["MERs"]. columns ["customerid"], custds. tables ["orders"]. columns ["customerid"]);
Foreach (datarow prow in custds. Tables ["MERs"]. Rows)
{
Console. writeline (prow ["mermerid"]);
Foreach (datarow crow in prow. getchildrows (custorderrel ))
Console. writeline ("\ t" + crow ["orderid"]);
}
SQL Server decimal type
Dataset uses the. NET Framework data type to store data. For most applications Program These types provide a convenient representation of data source information. However, if the data type in the data source is SQL Server decimal, this representation may cause problems .. Net Framework decimal data type can have up to 28 valid bits, while SQL Server decimal data type can have 38 valid bits. If the precision of the SQL Server decimal field is determined to be greater than 28 Characters During the fill operation, the current row will not be added to the datatable. The fillerror event will occur, which enables you to determine whether precision loss will occur and make appropriate responses. For more information about the fillerror event, see use dataadapter event. To obtain the SQL Server decimal value, you can also use the sqldatareader object and call the getsqldecimal method.
Use sqlcommand during the update process to change the dataset record
The following example uses the derived class oledbdataadapter to update the data source. This example assumes that you have created an oledbdataadapter and a dataset.
The following example uses the derived class oledbdataadapter to update the data source. This example assumes that you have created an oledbdataadapter and a dataset.
Public dataset createcmdsandupdate (Dataset mydataset, string myconnection, string myselectquery, string mytablename)
{
Oledbconnection myconn = new oledbconnection (myconnection );
Oledbdataadapter mydataadapter = new oledbdataadapter ();
Mydataadapter. selectcommand = new oledbcommand (myselectquery, myconn );
Oledbcommandbuilder custcb = new oledbcommandbuilder (mydataadapter );
Myconn. open ();
Dataset custds = new dataset ();
Mydataadapter. Fill (custds );
// Code to modify data in dataset here
Mydataadapter. Update (custds, mytablename );
Myconn. Close ();
Return custds;
}
The following instance creates a sqldataadapter and sets the selectcommand and insertcommand attributes. Assume that a sqlconnection object has been created.
Public static sqldataadapter createcustomeradapter (sqlconnection conn)
{
Sqldataadapter da = new sqldataadapter ();
Sqlcommand cmd;
// Create the selectcommand.
Cmd = new sqlcommand ("select * from MERs" +
"Where Country = @ country and city = @ City", Conn );
Cmd. Parameters. Add ("@ country", sqldbtype. nvarchar, 15 );
Cmd. Parameters. Add ("@ City", sqldbtype. nvarchar, 15 );
Da. selectcommand = cmd;
// Create the insertcommand.
Cmd = new sqlcommand ("insert into MERs (customerid, companyName)" +
"Values (@ customerid, @ companyName)", Conn );
Cmd. Parameters. Add ("@ customerid", sqldbtype. nchar, 5, "customerid ");
Cmd. Parameters. Add ("@ companyName", sqldbtype. nvarchar, 40, "companyName ");
Da. insertcommand = cmd;
Return da;
}
The following instance creates a sqldataadapter and sets the selectcommand and deletecommand attributes. Assume that a sqlconnection object has been created.
Public static sqldataadapter createcustomeradapter (sqlconnection conn)
{
Sqldataadapter da = new sqldataadapter ();
Sqlcommand cmd;
Sqlparameter parm;
// Create the selectcommand.
Cmd = new sqlcommand ("select * from MERs" +
"Where Country = @ country and city = @ City", Conn );
Cmd. Parameters. Add ("@ country", sqldbtype. nvarchar, 15 );
Cmd. Parameters. Add ("@ City", sqldbtype. nvarchar, 15 );
Da. selectcommand = cmd;
// Create the deletecommand.
Cmd = new sqlcommand ("delete from MERs where customerid = @ customerid", Conn );
Parm = cmd. Parameters. Add ("@ customerid", sqldbtype. nchar, 5, "customerid ");
Parm. sourceversion = datarowversion. Original;
Da. deletecommand = cmd;
Return da;
}
The following instance creates a sqldataadapter and sets the selectcommand and updatecommand attributes. Assume that a sqlconnection object has been created.
Public static sqldataadapter createcustomeradapter (sqlconnection conn)
{
Sqldataadapter da = new sqldataadapter ();
Sqlcommand cmd;
Sqlparameter parm;
// Create the selectcommand.
Cmd = new sqlcommand ("select * from MERs" +
"Where Country = @ country and city = @ City", Conn );
Cmd. Parameters. Add ("@ country", sqldbtype. nvarchar, 15 );
Cmd. Parameters. Add ("@ City", sqldbtype. nvarchar, 15 );
Da. selectcommand = cmd;
// Create the updatecommand.
Cmd = new sqlcommand ("Update MERs set customerid = @ customerid, companyName = @ companyName" +
"Where customerid = @ oldcustomerid", Conn );
Cmd. Parameters. Add ("@ customerid", sqldbtype. nchar, 5, "customerid ");
Cmd. Parameters. Add ("@ companyName", sqldbtype. nvarchar, 40, "companyName ");
Parm = cmd. Parameters. Add ("@ oldcustomerid", sqldbtype. nchar, 5, "customerid ");
Parm. sourceversion = datarowversion. Original;
Da. updatecommand = cmd;
Return da;
}
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.