Ado. NET database programming is more complex, the numerous classes, objects, attributes, methods make every programmer feel annoyed. The purpose of this paper is to introduce the main contents of ASP.net Beta2 database programming, and to help programmers understand the essence of ado.net database programming in the quickest.
First, Managed Providers
If you are a beginner, you may ask, what is "Managed Providers"?
Managed providers provides a simple way to connect and access a database, a bit like a database connection, and certainly a lot better than that. Managed providers provides OLE DB and SQL Server two programming interfaces. Because SQL Server is Microsoft's own product, it specifically provides an interface to SQL Server that is more efficient than using OLE DB to access SQL Server.
Namespaces
All of the example programs in this article need to use the following namespaces:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
Connection
In order to connect to the database, you must use OleDbConnection:
Dim objConn as New OleDBConnection
("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\sff\site\db\users.mdb")
Of course, you can also use the specific connection method as a variable, after connecting the database, you must open the database:
objConn.Open()
In this way, you can use the database, generally at the end, we all request to close the database connection:
objConn.Close()
objConn=Nothing
Command
Once you have connected the database, you can send commands to operate the database, and OleDbCommand allows you to send commands to manipulate the database. Based on the SQL statement sent, we can do almost everything to the database.
Dim objCmd as New OleDbCommand("SELECT * From users", objConn)
The above statement establishes the command and, depending on the custom, you can use the following methods:
Dim objCmd as New OleDbCommand()
objCmd.Connection = objConn
objCmd.CommandText = "SELECT * FROM users"
You can also do this:
Dim objCmd as New OleDbCommand(SQL String, connection string)
Looking closely at the above statement, we find that when defining OleDbCommand, you can use both the database connection OleDbConnection and the database connection statement. The above code does not execute the SQL statement, and now we see how to do it:
ExecuteNonQuery
When performing operations that do not return data, we can use the above methods, such as inserting data, updating data, and so on, specifically:
objCmd.ExecuteNoQuery
ExecuteReader
When we need a data reader, we can use the above methods to do this:
Dim objRd as OleDbDataReader
objRd = objCmd.ExeuteReader
ExecuteScalar
Use the ExecuteScalar method to obtain a single return of data, such as statistics on the data.