ASP. NET database operations and Data Display Technology

Source: Internet
Author: User

Two Core Components of ADO. Net:. NET Framework data provider and Dataset
Data providers include connection objects, command objects, datareader objects, and dataadapter objects. They are used to connect to data sources, execute SQL statements, and retrieve data, the retrieved data can be processed directly or put into the DataSet object.
Dataset includes a set of able objects and datarelation objects.
The datareader object reads only forward and read-only data streams from the data source.
The dataadapter object performs various operations on the data source, including updating and inserting data.

1. connection object
ADO. NET provides four Connection Methods: system. data. oledb. oledbconnection, system. data. sqlclient. sqlconnection, system. data. ODBC. odbcconnection, system. data. oracleclient. oracleconnection, the most common sqlconnection
For example:
Sqlconnection conn = new sqlconnection ("Server = localhost; uid = sa; Pwd = 123456; database = product ");
Conn. open ();
Conn. Close ();
Attribute:
Connectionstring connection string
Obtain the name of the current database or the database to be used.
State connection status
Name of the SQL server instance connected to datasource
Method:
Open
Close
Createcommand is created and a command object associated with the connection is returned.
Begintransaction
Changedatabase Change Database

2. sqlcommand object
String strsql = "";
Sqlcommand cmd = new sqlcommand (strsql, Conn );
Attribute:
Commandtext sets or returns command strings, tables, stored procedures, and SQL statements for the data source.
Commandtype
State command object status
Method:
Cancel cancels the asynchronous execute method. If the execute method does not specify the adrunasync parameter, an error is returned.
Execute
Executereader: Execute the command to return the line
Executenonquery: Number of affected records returned
Executereader: returns a typed datareader object.
Executescalar: The executed SQL statement should return a result, such as the number of records and server time.
Executexmlreader:

3. datareader object
The simplest way to read a database is to read data only forward, but not write data. There is only one row at a time.
Usage: first open the database, create a command object, execute SQL statements, and return a datareader object using the executereader method.
Access Database: oledbdatareader mydr = cmd. executereader ();
SQL Server: sqldatareader myreader = cmd. executereader ();
Attribute:
Isclosed: Status
Fieldcount: number of fields
Item :( {name, ordinal}) obtains or sets the field content. name indicates the field name, and ordinal indicates the field number.
Recordsaffected: obtains the number of affected rows.
Method:
Close ()
Getname (ordinal): obtains the field name in column ordinal + 1.
Getvalue (ordinal): Get the content of the ordinal + 1 column
Isdbnull (orderianl): whether it is null
Read (): reads the next data. If no value is returned, false is returned.
String connstr = "Server = ae5ec3e392d24e2 \ sqlexpress; database = educational administration system; uid = tjt; Pwd = 123456 ";
Sqlconnection conn = new sqlconnection (connstr );
Conn. open ();
If (conn. State = connectionstate. open)
{
Response. Write ("the connection has been opened! ");
}
String strsql = "select * from student ";
Sqlcommand cmd = new sqlcommand (strsql, Conn );
Sqldatareader myreader = cmd. executereader ();
Response. Write (myreader. fieldcount );
While (myreader. Read ())
{
Response. Write (myreader [0]. tostring () + "" + myreader [1]. tostring ());
}
Response. Write ("Data Reading is complete! ");

4. dataadapter and DataSet object
The dataadapter data adapter is a component of the ADO. Net hosting provider (a group of objects used to communicate between data sources and datasets), used to exchange data between data sources and datasets.
Dataset dataset is the core component of ADO. Net that supports disconnected data access ., It is the temporary cache area for database retrieval records.
The dataadapter object can create and initialize a data table (datatable), execute SQL commands on the data source, and combine them with the DataSet object to provide the DataSet object to access data, which can be considered as the core operation of the DataSet object, it is the communication medium between a DataSet object and a data operation object.
Workflow: the connection object establishes a connection with the data source, and the command object operates SQL commands to access data. The accessed data is returned to the dataadapter object through the connection object, then, the dataadapter object puts the data into the generated able object, and adds the datatable object in the dataadapter object to the datatables object in the DataSet object.
Access: oledbdataadapter myda = new oledbdataadapter ("SQL string", Conn );
Sqlserver: sqldataadapter mysda = sqldataadapter ("SQL string", Conn );

Attribute:
Deletecommand: gets or sets an SQL statement or stored procedure to delete records from the data source.
Insertcommand:
Selectcommand:
Updatecommand:
Tablemapping: sqldataadapter is used to map query results to dataset information sets.
Method:
Fill: executes the query stored in selectcommand and stores the result in the datatable.
Fillschema: obtains schema information for queries stored in selectcommand. column names and Data Types

Dataset is similar to a small relational database and contains one or more able objects.
The datatable object contains the set of datarow objects, the set of datacolumn objects, and the set of Constraint Objects, which respectively store the information of rows, columns, and constraints.
Dataset mydata = new dataset ();
Attribute:
Haserrors: whether it contains an error
Tables: Check the existing able object
Method:
Clear: Clear, faster than deleting and recreating
Clone: Create a new dataset by cloning the structure.
Copy: Copy structure and data

String connstr = "Server = ae5ec3e392d24e2 \ sqlexpress; database = educational administration system; uid = tjt; Pwd = 123456 ";
Sqlconnection conn = new sqlconnection (connstr );
Conn. open ();
If (conn. State = connectionstate. open)
{
String strselectsql = "select * from student ";
Sqldataadapter SDA = new sqldataadapter (); // data adapter
SDA. selectcommand = new sqlcommand (strselectsql, Conn); // get data
Dataset DS = new dataset (); // Dataset
SDA. Fill (DS); // fill the dataset

Gridview1.datasource = Ds. Tables [0]. defaultview; // control binding
Gridview1.databind ();
Conn. Close ();
}

5. datatable object
A collection of metadata and data. Metadata is described by a collection of datacolumn objects and Constraint Objects. Data is included in a collection of datarow objects.
Rows: Get the set of rows that belong to the table
Columns: a set of columns in the table
Primarykey: indicates the primary key.
Tablename: mingc
Method:
Importrow: add the datarow parent in another able with the same schema
Select: returns an array of datarow objects sorted or filtered Based on the provided parameters.
Clear: Clear DT data
**************************************** ******
Data display:
Gridview control: Supports automatic format and paging.

Getxml: Convert to XML

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.