ADO.NET Database programming is more complex, a wide range of classes, objects, properties, and methods make every programmer feel annoyed. This article is intended to introduce the master of ASP.NET Beta2 database programming
To help programmakers understand the essence of ADO.NET database programming.
First, Managed Providers
What is "Managed Providers?
Managed Providers provides a simple way to connect and access the database, a bit similar to a database connection, of course, more than it. Managed Providers offers OLEDB and SQL
Server two programming interfaces. Because SQL Server is Microsoft's own product, therefore specializes in interface for SQL Server, using this interface to access SQL Server
The rate should be better than using OLEDB.
Namespaces
All examples of this article require the following namespaces:
<% @ Import namespace = "system.data"%> <% @ Import namespace = "system.data.sqlclient"%>
Connection
In order to connect to the database, you must use SqlConnection:
protected System.Data.SqlClient.SqlConnection conn; conn = new System.Data.SqlClient.SqlConnection (); conn.ConnectionString = "workstation id = JSB; packet size = 4096; user id = sa; data source = QDHL; persist security INFO = true; initial
Catalog = QDHL; Password = mm ";
Of course, you can also use the specific connection method as a variable, after connecting the database, you must open the database:
Cn.open ();
In this way, you can use the database, usually in the end, we all ask the database connection:
CONN.CLOSE ();
Command
After connecting the database, you can send a command to operate the database, SQLCommand allows the command to operate the database. According to the sent SQL statement, we can log
According to the library, almost all operations. protected system.data.sqlclient.sqlcommand comm;
SQLSTR = "Select * from mmsbase"; comm = new system.data.sqlclient.sqlcommand (SQLSTR, CONN);
The above statement is established, depending on the habits, the following methods can also be used: comm.connection = conn; comm.commandtext = "select * from mmsbase";
It can also be the case:
Comm = new system.data.sqlclient.sqlcommand (SQLSTR, CONN);
Carefully observe the above statement, we found that when defining SQLCommand, you can use the database to connect to the SQLCONNECTION and database connection statements at the same time. The above code does not perform SQ
L Statement, now let's see how it is performed:
RS = comm.executenonquery;
When performing operations that do not return data, we can use the above methods, such as inserting data, updating data, etc., such as RS = Comm.ExecutenonQuery;
ExecuteReader
When you need a Data Reader, we can use the above method, which is performed soon:
Protected system.data.sqlclient.sqldataReader rs; rs = comm.executeReader ();
ExecuteScalar
Use the ExecuteScalar method to get a single return data, such as statistics of data. RS = comm.executescalar (); Data Reader
SqlDataReader is an object that is specifically used to read data. This object cannot be done other database operations in addition to reading data. Although it is simple, it is used to browse data
The efficiency is very high. Protected system.data.sqlclient.sqldatareader rs; rs = comm.executeReader (); while (rs.read ()) {mmsimg.imageurl = RS ["mms_img"]. Tostring ();}
The above statement reads the first field of the return result of Command, which is characteristic data. We can use other methods to get various types of data:
GetBoolean (X)
Getbyte (x)
Gettes (x)
GetChar (x)
GetChars (x)
GetDataTypename (X) - acquisition data type
GetDateTime (x)
GetDecimal (x)
GetDefaultStream (x)
GetDouble (x)
GetfieldType (x)
Getfloat (x)
GetGUID (X)
GetInt16 (x)
GetInt32 (x)
Getint64 (x)
GetName (x) - get a field name
Getordinal (Name) - get field serial number according to the field name
GetString (x)
GetTimeSpan (X)
GetValue (x)
GetValues (Values ())
The above methods are Command to return data.
Data adapter
SqlDataAdapter obtain data and build a bridge between the data and the DataSet, you can use this: protected System.Data.SqlClient.SqlDataAdapter NewsAdap; NewsAdap = new System.Data.SqlClient.SqlDataAdapter ( "select top 8 * from MmsBase", conn);
The implementation method is a bit similar to SQLCommand. SqlDataAdapter can populate DataSet or modify the data and then submit it to achieve modifications to concrete data:
QDHLTABLE = New DataSet (); newsadap.fill (QDHLTABLE, "MMSBase");
The above statement implements the data of the UserS table acquired by the SQL statement to the DataSet.
Mappings
Mappings can implement columns of DataSet:
Newsadap.tableMappings.add ("Adbtable", "Users");
Command Builder
In the next chapter we can see the specific use and powerful features of Command Builder.
Exercise: