Namespace in ASP. Net

Source: Internet
Author: User

Namespace in ASP. Net

I often use Namespace
Copy codeThe Code is as follows: <% @ Import Namespace = "System. Data" %>
This is the Namespace provided for us by reference. This is different from ASP. We must first reference the Namespace related to our operations in ASP.net before using the corresponding functions. To put it bluntly, a Namespace is a component.
This is an advanced application of ASP.net.

Below I will briefly list some commonly used namespaces

Copy codeThe Code is as follows:
<% @ Import Namespace = "System. Data" %> used to process Data
<% @ Import Namespace = "System. Data. ADO" %> used when using ADO.net;
<% @ Import Namespace = "System. Data. SQL" %> dedicated to SQL Server databases
<% @ Import Namespace = "System. Data. XML" %> you do not need to view
<% @ Import Namespace = "System. IO" %> used for File Processing
<% @ Import Namespace = "System. Web. Util" %>
<% @ Import Namespace = "System. Text" %> used for Text Encoding

What database operations require

After explaining the Namespace, we can officially discuss database applications.
From the above, we can see that we need to reference the following two namespaces when operating the database.
Copy codeThe Code is as follows:
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SQL" %>

In fact, System. data. SQL can use System. data. instead of ADO, SQL is dedicated to SQL Server. ADO can support any database (as long as the corresponding driver exists on the host, such as Access, Mysql, and Oracle ).

Both ADO and SQL have several basic objects for operations.
Copy codeThe Code is as follows:
Connections is linked to a database to facilitate subsequent applications (similar to Connections in ADO)
Where Commands executes SQL statements
DataReader reads the data returned after execution
DataSet stores data with powerful functions.
DataSetCommand executes SQL statements and saves data to DataSet

The most difficult thing to understand here is DataSet. Let's leave it alone.

Connections (SQLConection or ADOConnection)

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

Copy codeThe Code is 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 =; database = aspcn ";
SQLConnection MyConnection = new SQLConnection (strProvider );
}
</Script>


We have established a Connection named MyConnection, as if we opened a Connection with ADODB. Connection in ASP. This Connection will be used in Command or DataSetCommand.

Some of its useful attributes and methods are:
Copy codeThe Code is as follows:
ConnectionString gets or sets the connection statement of the database.
ConnectionTimeout gets or sets the maximum time for connecting to the database, which is also the timeout time.
DataBase gets or sets the name of the DataBase to be opened on the DataBase server.
DataSource gets or sets the DSN. No stranger to everyone :)
Password
UserID to obtain or set the login name
State to obtain the current join status
Open () Open join
Close () Close the connection
Clone () to Clone a join.

Let's also look at their usage through a small example:
Copy codeThe Code is 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)
In the above program, we open a connection. here we need to use this. The example is better:

Copy codeThe Code is 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 =; database = aspcn ";
String strIndex = "select * from aspcn where purview = 'webmaster '";
SQLConnection MyConnection = new SQLConnection (strProvider );
SQLCommand MyCommand = new SQLCommand (strIndex, MyConnection );
MyConnection. Open (); // Open the connection
MyCommand. ExecuteNonQuery (); // execute the SQL statement without returning any records
MyConnection. Close ();
}
</Script>
In the above example, two parameters (strIndex and MyConnection) are referenced when the SQLCommand object is created. From the source program, we can also see that strIndex represents the executed SQL statement, myConnection is a previously established connection. then we need to open MyConnnection and then execute this SQL statement.
Here we use the ExecuteNonQuery () method to execute the statement. In this way, the record set is not returned, but the number of affected records is returned.

Here we can also open and close the database.

Copy codeThe Code is as follows:
StringstrProvider = "server = localhost; uid = sa; pwd =; database = 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 result is the same as the previous one. Therefore, there are many ways to execute an SQL statement. There are more than two types. We will learn DataSetCommand later, and there are N methods to open it. :) this depends on your habits and program requirements ;)

Let's take a look at the common methods and attributes of Command.
Copy codeThe Code is as follows:
ActiveConnection
Name of the SQL statement or stored procedure (StoredProcedure) executed by CommandText
Maximum CommandTimeout execution time
CommandType Command operation types (StoredProcedure, Text, TableDirect), default Text
Parameters
Execute () Execute SQL statements or stored procedures
ExecuteNonQuery () is the same as above. The difference is that the record set is not returned.
Clone () Clone Command

Let's take an example:
Copy codeThe Code is as follows:
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;
MyCommand. CommandType = CommandType. Text;


What is the default namespace ASP in aspnet? How to use it?

A namespace is a package. Each namespace contains its own methods and objects. You must reference a method or object. If this parameter is not referenced, it cannot be used. For example, you need a able object. Then you must apply the Data namespace, that is, the using System. data; otherwise, the DataTable will report an error. The following will display a red line. You can also press DataTable and place the mouse over the keyboard. The shortcut key Shift + Alt + F10 will automatically guide the package.

In aspnet, What Is namespace? What is the role?

Namespace can be understood as a logical function library (which contains various functions). It corresponds to assembly, which is a physical function library (that is, DLL files ).
Namespace is hierarchical, which is similar to the chapter in the book. This makes it easier to use classes and objects in namespace. As shown in the following example: dim abc As System. IO. StreamReader defines abc As the StreamReader class in the IO sub-space under the system namespace.

Since each namespace is actually saved in each assembly, you must first reference the assembly where it is located before using the namespace.
You can also write the import namespace as needed. In this way, you can omit the long path when using classes and objects in namesapce. For example, the preceding example can be written as follows:
Import System. IO
Dim abc as StreamReader

With namespace, classes and functions with the same name are allowed, because they are located in different namespaces and do not conflict with each other! For example, System. web. there are TextBox classes in the UI space, while System. windows. there is also a TextBox class in the Forms space. The names of these two classes are exactly the same (in fact, their functions are similar), but when you use them, the system can tell which one it is. This is the relationship between namespaces!

Of course, in addition to the system namespace, developers can define their own namespace. In fact, when you compile a solution, the system also uses your project name to generate a namespace and put your class (in fact, each web page is a class) put all under this namespace!

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.