Asp. namespace analysis and use examples _ practical skills of namespaces in net

Source: Internet
Author: User

With regard to the use of namespace (namespaces), I often

Copy Code code as follows:
<% @ Import namespace= "System.Data"%>

, this is in reference to provide us with the namespace, which is different from the ASP, we asp.net must first refer to our operations related to the namespace to use the appropriate functionality. In fact plainly, a namespace; is a component.
This is an advanced application of asp.net.

Here's a simple list of some of the common namespace

Copy Code code as follows:

<% @ Import namespace= "System.Data"%> used when 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"%> without looking at processing XML
<% @ Import namespace= "System.IO"%> When 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.
As can be seen from the above, we operate the database, we need to quote the following two namespace

Copy Code code as follows:

<% @ 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-specific, ADO can support any database (as long as the corresponding driver on the host on the line, such as access,mysql,oracle, etc.).

Whether it's ADO or SQL, they have several basic objects for operation

Copy Code code as follows:

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 the dataset, we don't care about him first.

Connections (sqlconection or adoconnection)

Its main task is to establish a connection with the database server

Copy Code code as follows:

<% @ 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";
SqlConnection myconnection=new SqlConnection (Strprovider);
}
</script>


Above we established a join called myconnection, as if we had opened a connection with adodb.connection in ASP, which we would use in command or DataSetCommand.

Some of its useful properties and methods have

Copy Code code as follows:

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.

We also use a small example to see their usage:

Copy Code code as follows:

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:

Copy Code code as follows:

<% @ 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, and we can see from the source program that STRINDEX represents the executed SQL statement. MyConnection is the connection we established earlier. Then we have to open the myconnnection first and then execute the SQL statement.
We're doing this here with 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.

Copy Code code as follows:

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 not only two, we learned the DataSetCommand, the Open method is N: This will need to see your habits and procedures of the requirements;

Let's take a look at command's common methods and properties.

Copy Code code as follows:

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:
Copy Code code as follows:

String myselectquery = "SELECT * from Categories ORDER by CategoryID";
stringmyconnectstring= "userid=sa;password=;d atabase=northwind;server=mysqlserver";
SQLCommand mycommand = new SQLCommand (mySelectQuery);
Mycommand.activeconnection = new SqlConnection (myconnectstring);
Mycommand.commandtimeout = 15;
myCommand.CommandType = CommandType.Text;

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.