In the previous program we saw that I used to <% @ Import namespace= "System.Data"%>
With m$ for us to provide the namespace, which is different from the ASP, we must first quote in asp.net with our operations have
namespace to use the appropriate function after the shutdown. In fact plainly, a namespace; is a component.
This is an advanced application of asp.net, which I will explain later in the chapter. (But to write there, time ...)
)
Here's a simple list of some of the common namespace
<% @ Import namespace= "System.Data"%> processing data
<% @ Import namespace= "System.Data.ADO"% > Use ado.net; When you use the
<% @ Import namespace= "System.Data.SQL"%> SQL Server database dedicated
<% @ Import namespace= "System.Data.XML"%> do not need to look at processing XML
<% @ Import namespace= "System.IO"%> Processing files
<% @ Import namespace= "System.Web.Util"%> email when you use
<% @ Import namespace= "System.Text"%> text encoding
What you need to manipulate the database
Explaining the namespace, we can formally discuss the application of the database. From the above we can see that we operate
Database, we need to quote the following two namespace
In fact, System.Data.SQL can be replaced with System.Data.ADO, SQL is SQL Server-specific, ADO can
Supports any database (as long as there is a corresponding driver on the host), such as Access,mysql,oracle.
Here, because the database of flying knives is SQL Server, you could have used ADO, but think m$ separate the SQL out
Come on, why not? As for how much benefit it can bring, the flying knife has not been tested, the SQL Server is certainly more than ADO
A little better.
Whether it's ADO or SQL, they have several basic objects for operation
Connections links to a database for later applications (like Connections in ADO)
Where to execute SQL statements Commands
DataReader read the contents of the data returned after execution
DataSet storage data, powerful, we will specifically explain
DataSetCommand executes the SQL statement and stores the data in a dataset
Perhaps the most difficult thing to understand here is dataset, let's not take care of him first, take the soft surgery
Connections (sqlconection or adoconnection)
Its main task is to establish a connection with the database server
Above we set up a connection called myconnection, as if we were using adodb.connection in ASP
A join was opened. This connection we will use in command or DataSetCommand.
Some of its useful properties and methods have
ConnectionString Gets or sets the statement that links the database
ConnectionTimeout Gets or sets the maximum time to connect to the database, and also the timeout time
DB Gets or sets the name of the database to open on the database server
DataSource Get or Set DSN, we are not unfamiliar bar:
Password get or set a password
UserID Get or Set login name
State gets the status of the current join
Open () opens the join
Close () off join
Clone () clones a join. (Oh, sheep can connection I also can)
We also use 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.isolationlevel = isolationlevel.readcommitted
Commands (SQLCommand or Adocommand)
We have a connection in the program above, and here we need to use this, and look at the examples better:
<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.SQL"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{
Stringstrprovider= "server=localhost;uid=sa;pwd=;d ATABASE=ASPCN";
String strindex= "select * from ASPCN where purview= ' webmaster '";
SqlConnection myconnection=new SqlConnection (Strprovider);
SQLCommand mycommand = new SQLCommand (strindex,myconnection);
Myconnection.open (); Open Join
Mycommand.executenonquery (); Execute SQL, but not return any records
Myconnection.close ();
}
</script>
In the example above, we referenced two parameters (Strindex,myconnection) When we created the SqlCommand object.
From the source program we can also see that strindex represents the execution of the SQL statement, MyConnection is our first
The previously established connection. Then we have to open the myconnnection first, and then execute this SQL statement. We're here.
is executed using the ExecuteNonQuery () method, which does not return the recordset, but returns the number of records affected
。
Here we can also do this by opening and closing the database.
Stringstrprovider= "server=localhost;uid=sa;pwd=;d ATABASE=ASPCN";
String strindex= "select * from 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 are the same as before. So there are a number of ways to execute a single SQL statement. And there are more than two, after we
To learn the DataSetCommand, the Open method is n species: It depends on your habits and procedures of the requirements of
;)
Let's take a look at command's common methods and properties.
ActiveConnection Gets or sets the join connections
SQL statement or stored procedure (storedprocedure) name executed by CommandText
CommandTimeout the maximum time to execute
CommandType command operation type (storedprocedure,text,tabledirect) three, default Text
Use when Parameters operation stored procedure
Execute () executes the SQL statement or stored procedure
ExecuteNonQuery () ditto, except that the recordset is not returned
Clone () Cloning command
See also an example:
String myselectquery = "SELECT * from Categories ORDER by CategoryID";
stringmyconnectstring= "userid=sa;password=;d atabase=northwind;server=mysqlser
Ver ";
SQLCommand mycommand = new SQLCommand (mySelectQuery);
Mycommand.activeconnection = new SqlConnection (myconnectstring);
Mycommand.commandtimeout = 15;
myCommand.CommandType = commandtype.text;</font >
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