Tutorials to .com

Tutorials to .com » Dotnet » Database » Access the database through ADO.NET

Access the database through ADO.NET

Print View , by: iSee ,Total views: 6 ,Word Count: 1188 ,Date: Sun, 14 Jun 2009 Time: 8:46 PM

access the database through ADO.NET

Author: unknown
-------------------------------------------------- ------------------------------



ADO.NET database access, whether from the syntax of the steps to view, or from the view of style and design objectives, ADO.NET and ADO are significantly different. Through ADO in asp to access the database, generally through the following four steps:
1, to create a link to the database, that is, ADO.Connection;
2, a data collection query, that is, the implementation of SQL, a Recordset;
3, collection of data required to operate;
4, the closure of the data link.
In ADO.NET, these steps have changed a lot. ADO.NET one of the most important is the concept of DataSet. DataSet is not dependent on an independent data set database. The so-called independence, is this: even if the data link disconnect, or shut down the database, DataSet is still available. If you use ASP there record collection of non-connected (Connectionless Recordset), then the DataSet is the most thorough of the technology alternatives.
With DataSet, then, ADO.NET database access steps on the corresponding changed:
1, to create a database link;
2, the request of a record collection;
3, the temporary record set to DataSet;
4, if necessary, return to Step 2; (DataSet can hold multiple data collection)
5, the closure of the database link;
6, in the DataSet to make the required operation.
DataSet is used internally to describe the xml data. Because xml is a platform-independent, language-independent data description language, and can describe the complex data relationships of data, such as father and son relationship between the data, it can actually accommodate the DataSet with data on the complex relationship and it is no longer dependent on the database link .
ADO.NET Object Model Overview
ADOConnection
There are many targets of ADO.NET. We first take a look at the most basic and most commonly used in a few. First of all, take a look at ADOConnection. And ADO corresponding to the ADODB.Connection object, ADOConnection maintain a link to the database. In order to use ADO.NET objects, we need to introduce two NameSpace: System.Data and System.Data.ADO, use the Import command on ASP.NET can:
<% @ Import Namespace = "System.Data"%>
<% @ Import Namespace = "System.Data.ADO"%>
And similar to ADO's Connection object, ADOConnection object has two methods of Open and Close. The following example shows how to connect to the local MS SQL Server's Pubs database.
<% @ Import Namespace = "System.Data"%>
<% @ Import Namespace = "System.Data.ADO"%>
<%
Set up connection string''...
Dim strConnString as String
strConnString = "Provider = SQLOLEDB; Data Source = (local);" & _
"Initial Catalog = pubs; User ID = sa"
Create Object ADOConnection''
Dim objConn as ADOConnection
objConn = New ADOConnection
ADOCOnnection''set up the connection string object objConn.ConnectionString = strConnString
objConn.Open ()''''data link open source database operations omitted objConn.Close ()''close the Data Link objConn = Nothing''to remove the object%>
ADO above code and no much difference. It should be mentioned, ADO.NET database connection provides two methods: ADO and SQL mode approach. Here, we approach through ADO to connect to the database. On the establishment of the database connection details, we will be back about the length.
ADODatasetCommand
Another had referred to ADO.NET is ADODatasetCommand. Responsible for the creation of this object that we mentioned earlier DataSet object. Another important object of ADO.NET is the Dataview, it is a view of the DataSet. Still remember the DataSet can accommodate a variety of complex data relationships it? Through Dataview, we can limit the DataSet data to a particular scope.
The following code shows how to use DataSet to fill ADODatasetCommand data:
Create a SQL string''Dim strSQL as String = "SELECT * FROM authors"
Create Object ADODatasetCommand''and Dataset
Dim objDSCommand as ADODatasetCommand
Dim objDataset as Dataset = New Dataset
objDSCommand = New ADODatasetCommand (strSQL, objConn)
Data to fill Dataset''
Data set''and named the "Author Information"
objDSCommand.FillDataSet (objDataset, "Author Information")
Showing Dataset
Front we have the data ready. Let us look at how to display the data in the Dataset. In ASP.NET, the show used the DataSet control is DataGrid, it is one of the ASP.NET controls HTML can be a good performance as a form, the form can control the appearance, or even show the page. Here we simply use it:
<asp:DataGrid id="DataGridName" runat="server"/>
The remaining task is to bind the Dataset to the DataGrid. ASP.NET binding is an important concept, we will also explain the text. Generally speaking, you need a Dataview bind to DataGrid, but not directly bind Dataset. Fortunately, there is a default Dataset of Dataview, we put it the following DataGrid Binding:
MyFirstDataGrid.DataSource = _
objDataset.Tables ( "Author Information"). DefaultView
MyFirstDataGrid.DataBind ()
Complete code (122301.aspx)
<% @ Import Namespace = "System.Data"%>
<% @ Import Namespace = "System.Data.ADO"%>
<%
Set up connection string''...
Dim strConnString as String
strConnString = "Provider = SQLOLEDB; Data Source = (local);" & _
"Initial Catalog = pubs; User ID = sa"
Create Object ADOConnection''
Dim objConn as ADOConnection
objConn = New ADOConnection
ADOCOnnection''set up the connection string object objConn.ConnectionString = strConnString
objConn.Open ()''''open data link to create SQL string Dim strSQL as String = "SELECT * FROM authors"
Create Object ADODatasetCommand''and Dataset
Dim objDSCommand as ADODatasetCommand
Dim objDataset as Dataset = New Dataset
objDSCommand = New ADODatasetCommand (strSQL, objConn)
Data to fill Dataset''
Data set''and named the "Author Information"
objDSCommand.FillDataSet (objDataset, "Author Information")
objConn.Close ()''close the Data Link objConn = Nothing''to remove the object Authors.DataSource = _
objDataset.Tables ( "Author Information"). DefaultView
Authors.DataBind ()
%>
<HTML>
<BODY>
<asp:DataGrid id="Authors" runat="server"/>
</ BODY>
</ HTML>


.Net Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.