A brief analysis and use examples of Namespace in ES0en. Net

  • 2021-01-03 20:52:12
  • OfStack

Regarding the use of Namespace(namespace), I often use it

< % @ Import Namespace="System.Data" %>

This is a reference to the Namespace provided for us, which is different from ASP, we must refer to the Namespace related to our operation before we can use the corresponding function. To put it bluntly, 1 Namespace; It's 1 component.
This is an advanced application of ES12en.net.

Here is a simple list of 1 common Namespace


< % @ Import Namespace="System.Data" %> When you're dealing with data
< % @ Import Namespace="System.Data.ADO" % > use ADO.net ; Used when
< % @ Import Namespace="System.Data.SQL" %> SQL Server Database-Specific
< % @ Import Namespace="System.Data.XML" %> Don't look at it XML use
< % @ Import Namespace="System.IO" %> When you're working with files
< % @ Import Namespace="System.Web.Util" %> You'll use it when you send emails
< % @ Import Namespace="System.Text" %> It's used for text encoding

What you need to manipulate the database

Now that we've covered Namespace, we can officially talk about using a database.
As you can see from the above, we are working with the database, and we need to refer to the following two Namespace


< % @ Import Namespace="System.Data" %>
< % @ Import Namespace="System.Data.SQL" %>

System. Data. SQL can be replaced with ES33en. Data. ADO, SQL is SQL Server dedicated, ADO can support any database (as long as the corresponding driver exists on the host, such as Access,Mysql,Oracle and so on).

Both ADO and SQL have several basic objects for operations


Connections Links to 1 A database for subsequent applications (similar ADO In the Connections)
Commands perform SQL Where the words are
DataReader Read the content of the data returned after execution
DataSet It stores data, it's very powerful, and we'll talk about that
DataSetCommand perform SQL statements , And store the data DataSet

Perhaps the most difficult to understand is DataSet, let's leave him alone.

Connections (SQLConection or ADOConnection)

Its main task is to establish a connection to 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=;database=aspcn";
SQLConnection MyConnection=new SQLConnection(strProvider);
}
</script>


Above we created a link called MyConnection, just as we opened a link with ES66en.Connection in ASP, which we will use in Command or DataSetCommand.

Some of its useful properties and methods are


ConnectionString  Gets or sets the statement that connects to the database
ConnectionTimeout  The maximum time to get or set a linked database is also the timeout period
DataBase  Gets or sets the name of the database to open on the database server
DataSource  Get or set DSN, You won't be surprised :)
Password  Gets or sets the password
UserID  Gets or sets the login name
State  Gets the current state of the join
Open()  Open the connection
Close()  Close connection
Clone()  cloning 1 A connection.

Let's also take a look at their usage with a small example:


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 have opened 1 join, here we need to use this, look at the example 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=;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(); // perform SQL , but does not return any records
MyConnection.Close();
}
</script>

In the example above, we used two parameters (strIndex,MyConnection) to create the SQLCommand object. We can also see from the source that strIndex represents the executed SQL statement, and MyConnection is the join we established earlier. Then we need to open MyConnnection and then execute the SQL statement.
We are using the ExecuteNonQuery() method here, which does not return the recordset, but only the number of records affected.

Here we can open and close the database and do the same.


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. So there are many ways to execute an SQL statement. And there are more than two, we learned DataSetCommand later, so N is the way to open it :) it depends on your habits and the requirements of the program;)

Let's start by looking at the common methods and properties of Command


ActiveConnection  Gets or sets the join Connections
CommandText  perform SQL Statement or stored procedure (StoredProcedure) The name
CommandTimeout  Maximum execution time
CommandType Command Type of operation (StoredProcedure,Text,TableDirect)3 Kind of , The default Text
Parameters  Used when operating a stored procedure
Execute()  perform SQL Statement or stored procedure
ExecuteNonQuery()  Ibid., the difference is that no recordset is returned
Clone()  cloning Command

Let's look at the same example:

< % @ Import Namespace="System.Data" %> When you're dealing with data
< % @ Import Namespace="System.Data.ADO" % > use ADO.net ; Used when
< % @ Import Namespace="System.Data.SQL" %> SQL Server Database-Specific
< % @ Import Namespace="System.Data.XML" %> Don't look at it XML use
< % @ Import Namespace="System.IO" %> When you're working with files
< % @ Import Namespace="System.Web.Util" %> You'll use it when you send emails
< % @ Import Namespace="System.Text" %> It's used for text encoding
0


Related articles: