ado| Basic Tutorials
The Ado.net tutorial on the network is not based on VS2003, I here to the former beginners of some of the code to share to everyone, so that still in the groping phase of the friends can learn a little smoother
First, data query
1, DataReader read-only forward data set
Dim cn as New sqlclient.sqlconnection
cn. ConnectionString = "server=develop;uid=sa;pwd=hz0222;database=crm2004"
cn. Open ()
Dim cmd as New sqlclient.sqlcommand
Cmd. Connection = cn
Dim I as Integer
Cmd.commandtext = "SELECT * FROM TABLE1"
Dim Dr as Sqlclient.sqldatareader
Dr = cmd. ExecuteReader
i = 0
Do While Dr. Read ()
FP1. Sheets (0). Cells (i, 0). Text = DR ("id")
FP1. Sheets (0). Cells (i, 1). Text = DR ("FName")
FP1. Sheets (0). Cells (i, 2). Text = DR ("Fage")
FP1. Sheets (0). Cells (i, 3). Text = DR ("FAdd")
i + 1
Loop
In this example, you first declare a SqlConnection object, use it to connect to the SQL Server database, and then apply for a SqlCommand object that executes an SQL query statement, and finally declares a SqlDataReader, Use it to store the dataset returned by the ExecuteReader method of the SqlCommand object
With DataReader heard quickly, I did not do the test, but on the internet to say that there is any basis, I also used to query data. The above example is used to query all the data in a table, and put them in a data control, with VB Code, C # large to almost
2. DataSet Data Set
The dataset can manage multiple recall sets: A DataTable, or ADO's Recordset object, see the following example:
' Bind data
Private Sub Binddata ()
' Request a connection string object
Dim cn as New sqlclient.sqlconnection
' Initialize the connection string
cn. ConnectionString = "server=localhost;uid=sa;pwd=hz0222;database=crm2004"
' Open the connection string
cn. Open ()
' Declare a data adapter
Dim da as New sqlclient.sqldataadapter ("SELECT * from T_cst_info", CN)
' Declare a dataset (can contain tables)
Dim DS as New DataSet
' Populate data table
Da. Fill (ds, "T_cst_info")
Ds. Tables (0). TableName = "Customer Profile"
Da. SelectCommand = New Sqlclient.sqlcommand ("SELECT * from T_base_zygl", CN)
Da. Fill (ds, "T_base_zygl")
Ds. Tables (1). TableName = "Staff file sheet"
Da. SelectCommand = New Sqlclient.sqlcommand ("SELECT * from T_base_bmqk", CN)
Da. Fill (ds, "T_base_bmqk")
Ds. Tables (2). TableName = "Departmental situation table"
' Bind to the DataGrid
Me.DataGrid1.DataSource = ds
End Sub
This code is similar to the above, just use the SqlDataAdapter to replace the SqlCommand Execute SQL query, SqlDataAdapter in the words of Microsoft is "data adapter", I now know the function is to execute SQL query, and populate the DataSet or DataTable object. In the example above, we've populated three DataTable
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