Author: Flying
About Namespace (Name Space) is common <% @ import namespace = "system.data"%>, this is the Namespace, which provides us, this is different from ASP, we win sp.net must first reference The corresponding features can be used after we operate the Namespace. In fact, it is white, a namespace; is a component I simply list some common Namespace.
<% @ Import namespace = "system.data"%> Used when processing data
<% @ Import namespace = "system.data.ado"%> Using ADO.NET;
<% @ Import namespace = "system.data.sql"%> SQL Server database dedicated
<% @ Import namespace = "system.data.xml"%> Do not look at XML to use
<% @ Import namespace = "system.io"%> Used when handling files
<% @ Import namespace = "system.web.util"%> When you send an email, you will use it.
<% @ Import namespace = "system.text"%> Text encoding
Operating database needs
Explain Namespace, we can formally discuss the application of the database. As can be seen from above, we operate the database, we need to reference the following two namespace
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sql"%>
In fact, System.Data.sql can be replaced with system.data.ado, SQL is SQL Server dedicated, ADO can support any database (as long as there is a corresponding driver on the host, such as Access, MySQL, Oracle, etc.), Here, because the database of the flying knife is SQL Server, you can use ADO, but think about M $ separate SQL separately, why don't you use it? As for how much benefits it can, the flying knife has not been tested, and SQL Server is definitely better than ADO.
Whether it is ADO or SQL, they all have several basic objects for operation.
Connections links to a database so that the following applications (similar to the connections in ADO)
Commands to execute sql statements
DataReader reads the data content returned after execution
DataSet stores data, powerful, we will explain
DataSetCommand executes the SQL statement and stores the data into DataSet
It may be the most difficult to understand this is Dataset, we will not take care of him first, first take soft knife.
Connections (SQLCONECTION or Adoconnection)
Its main task is to establish a link to the database server.
<% @ Page language = "c #"%>
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sql"%>
Public void Page_Load (Object SRC, Eventargs E)
{
StringstrProvider = "Server = localhost; uid = sa; pwd =; database = aspcn";
SqlConnection myconnection = new SqlConnection (STRPROVIDER);
}
script>
Above we established a connection called MyConnection, as if we have opened a connection with AdoDb.Connection in ASP. This connection will be used in Command or DataSetCommand.
Its useful properties and methods have
Connectionstring acquisition or set the statement of the link database
ConnectionTIMEOUT acquires or sets the longest time to connect the database, it is timeout
Database acquisition or set the database name to open on the database server
DataSource gets or sets DSN, everyone will not be unfamiliar :)
Password gets or sets your password
UserID acquisition or set up the login name
State gets the state of current connection
Open () open connection
Close () closing linkage
Clone () cloned a connection. (Oh, sheep can be CONNECTION I can also)
We also take a small example to see their usage:
SqlConnection myconnection = new sqlConnection ();
MyConnection.dataSource = "mysqlserver";
MyConnection.password = ""
MyConnection.Userid = "sa";
MyConnection.ConnectionTimeout = 30;
MyConnection.open ();
MyConnection.Database = "northwind";
MyConnection.issionLevel = isolationledLevel.Readcommitted
Commands (SqlCommand or Adocommand)
In the above program, we opened a connection, here we need to use this, see examples better:
<% @ Page language = "c #"%>
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sql"%>
Public void Page_Load (Object SRC, Eventargs E)
{
StringstrProvider = "Server = localhost; uid = sa; pwd =; database = aspcn";
String strindex = "select * from ask aspcn where purview = 'webmaster'";
SqlConnection myconnection = new sqlconnection (strprovider); sqlcommand mycommand = new sqlcommand (StrIndex, MyConnection);
MyConnection.Open (); // open connection
MyCommand.executenonQuery (); // Execute SQL, but do not return any records
MyConnection.Close ();
}
script>
In the above example, we reference two parameters (StrIndex, MyConnection) when building a SQLCommand object. From the source program, we can also see that STRINDEX represents the execution SQL statement, MyConnection is the jointly established joints. Then we will To open myConnnection first, then execute this SQL statement. We do this here is the executenonquery () method, which does not return record sets, just returns the number of affected records.
Here we open and close the database can also do this.
StringstrProvider = "Server = localhost; uid = sa; pwd =; database = aspcn";
String strindex = "select * from ask aspcn where purview = 'webmaster'";
SqlConnection myconnection = new SqlConnection (STRPROVIDER);
Sqlcommand mycommand = new sqlcommand (strindex, myconnection);
Mycommand.activeConnection.Open ();
Mycommand.executenonquery ();
Mycommand.activeConnection.Close ();
The results obtained were previously the same. So there are many ways to perform a SQL statement. And not only two, we learned DataSetCommand, then the method is N :) This requires you to see your habits and procedures;)
Let's take a look at the common methods and properties of Command.
ActiveConnection acquisition or set up links Connections
CommandText execution SQL statement or storedProcedure name
The maximum time for CommandTimeOut
CommandType Command's Type (StoredProcedure, TEXT, TABLEDIRECT), default Text
PARAMETERS is used when the storage process is stored
Execute () executes SQL statements or storage procedures
ExecutenonQuery () is the same, the difference is that the recordset is not returned
Clone () Clone Command
Look at one example:
String myselectQuery = "select * from categories order by categoryid";
StringmyConnectString = "UserId = sa; password =; database = northwind; server = mysqlserver";
Sqlcommand mycommand = new sqlcommand (MySelectQuery);
MyCommand.activeConnection = New SqlConnection; "MYCONNECTSTRING);
Mycommand.commandtimeout = 15;