Intimate contact asp.net (6) The use of Namespace (namespaces)

Source: Internet
Author: User
Tags join

About the use of Namespace (namespaces) in the previous program, we saw that I used <% @ Import namespace= "System.Data"%>, which is a reference m$ for us, which is different from ASP, We must first refer to the namespace associated with our operations before ASP.net can use the appropriate functionality. 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. As can be seen from the above, we operate the database, we need to quote 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-specific, ADO can support any database (as long as the corresponding driver on the host on the line, such as access,mysql,oracle, etc.), Here because the database of flying knives is SQL Server, could have used ADO, but think m$ separate SQL out, why not. As for how much benefit it can bring, the flying knife has not been tested, and SQL Server is certainly better than ADO.

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

<% @ 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 join with adodb.connection in ASP. 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, as well as the timeout time
DataBase Gets or sets the name of the database to open on the database server
DataSource Get or set DSN, everyone is not unfamiliar bar:
Password Get or set a password
Userid Get or set the login name
State Get the status of the current connection
Open () Open Join
Close () Close Join
Clone () Clone 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 (); file:// Open the connection
Mycommand.executenonquery (); file:// Row sql, but no records are returned
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.

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.

ActiveConnection Gets or sets the join connections
CommandText Executed SQL statement or stored procedure (storedprocedure) name
CommandTimeout The maximum time to execute
CommandType Command operation type (storedprocedure,text,tabledirect) Three, default Text
Parameters Use when manipulating stored procedures
Execute () Execute SQL statements or stored procedures
ExecuteNonQuery () Ditto, the difference is that the recordset is not returned
Clone () Clone command

See also an example:

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;</font >



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.