. NET database connection to objects in the

Source: Internet
Author: User



in learning vb.net video, several of these units spoke of. NET database design and connectivity. For the database connection, in fact, we are not unfamiliar, originally in the Red Book and computer room charge system, we have contact, but, in my impression, these knowledge of database connection is very vague. is smattering for database connection objects.

Looking back, I turned over a few examples of the Red Book, which talked about using the ADO control to connect to the database, which involves 7 database connection objects (Connection,command, Recordset,field,property,parameter, Error

So what are the objects that are involved in the. NET database connection that you are learning today?

In fact, it is not difficult to find, whether it is the previous ADO or the present. NET, in the database connection there are two immutable objects connection and command, then in. NET how they are used?


1.Connection

Database Connection object, responsible for connecting to the database

Specific use: There are different namespaces for different data providers (provider), but they are generally consistent, as shown in SQL Server for example:

The Imports ss=system.data.sqlclient ' declaration uses a namespace dim sconn as string,dbconn as SS. Sqlconnectionsconn= "Server=localhost; database=dbname;integrated security=sspi "' Connection string dbconn=new ss. SqlConnection (sconn) ' Instantiate connection object before use Dbconn.open () ' So now you can connect to the database to manipulate the data in the database.


2.Command

The database command object, which is mainly used to perform operations such as add, delete, modify, and query commands, can also execute pre-stored programs.

Depending on the database provider type, the command object can be divided into four classes, namely Sqlcommand,oledbcommand,odbccommand,oraclecommand.

When used, simply instantiate it to

Dim Sscmd as SS. Sqlcommandsscmd=new SS. SqlCommand (strsql,sconn) ' strSQL for command statements such as SELECT, sconn for connection characters

Of course, there are three common methods for command objects, in addition to these:

ExecuteNonQuery (): Returns the number of rows affected

Exectescalar (): Retrieves a single value from the database, returning the first row of the result

ExecuteReader (): Executes an SQL statement that returns a DataReader object

What does it mean when it comes to DataReader objects? Let's introduce the rest of the objects in. Net


3.DataReader

A simple data set that retrieves a read-only dataset from a data source that, when retrieving data, only allows read-only, forward-only reading of the data, cannot be returned, and that object can be created by retrieving data from the data source through the ExecuteReader () method in the Command object.

Its read () method is used to read the next record, and returns True if the data is read, otherwise false

On the basis of the above, look at its specific use:

Dim Ssreader as SS. SqlDataReader ' and above command is a reason strsql= "select * from Customers ORDER by LastName Asc,firstname ASC;" Ssreader=executereader () ' Execute SQL command ' starts reading records, the following FirstName and LastName are two column names, a total of one record, each loop once, return a record does while Ssreader.read ()    fn=system.convert.tostring (Ssreader ("FirstName"))    In=system.convert.tostring (Ssreader ("LastName"))    Me.listbox1.Items.Add (in,+ Fn) loop

So beyond that, in. When designing. NET data access, in order to be able to operate the database offline, the Recordset object in the ADO data connection is replaced with a dataset for the purpose of operating the database offline.

In ADO, the recordset becomes the DataSet object, then the. The DataSet object in NET is a DataSet!


4.DataSet

in layman's words, an offline Recordset is used to process data offline because the dataset provides an offline source of data (usually in tabular form) to ease the burden on the network.

, the use of the dataset in the process, but also need to rely on the DataAdapter object, then DataAdapter here in the end what role?

In fact, if we are careful to analyze, if the dataset in the client wants to be offline processing, it is said to act as a data source, but if it does not connect with the real data source, then the end of the effect of offline processing. So we have to build a bridge between the real data source and the dataset to enable communication between the two. Then the bridge is DataAdapter, and obviously the two are inseparable. Let's take a specific look at the object of DataAdapter.


5.DataAdapter

. NET, acting as a bridge between the data source and the dataset to retrieve and save data. So how did he achieve the communication between the two?

First, it connects to the data source through the database connection object (connection), and secondly, it uses the specified action to retrieve the data from the data source by using a database command object to send it to the dataset or from the dataset to the data source.

Through the above introduction, the DataSet and DataAdapter specific functions have a certain understanding, then how to coordinate with the use of it? Take a look at a small example:

Still operate on the basis of the above connection and command section code:

Dim adapter as SS. Sqldataadapterdim DS as System.Data.DataSet ' data from the data source is passed to the dataset in the following code sql= "SELECT * FROM Products;" Adapter=new SS. SqlDataAdapter (sscmd) ' command to be transferred is Ds=new System.Data.DataSet () ' Instantiation Datasetssconn.open () ' in Sscmd, the Open and close methods here can be omitted, The dataset automatically checks if the connection is successful Adapter.fill (DS) Ssconn.close () ' can use the following code to extract data from the dataset into the listbox ds.tables ("Table"). Tablename= ' products ' Dim row as system.data.datarow,name as stringfor each row in DS. Table ("Products"). Rows       Name=convert. ToString (row. Item ("ProductName"))       Me.listBox1.Items.Add (name) Next row

These are the objects that are commonly used in the connection to the ADO. NET database. By understanding the basic functionality and usage of these objects, we can take a closer look at the. NET database connection.








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.