This example, because in the introduction of ADO has been specially learned, again to review.
Master the case of connection strings.
The process is:
1, quote System.Data.SqlClient. The reference in Access is System.Data.OleDB. So there's a difference.
2, the relevant connection, adapter, data set.
3, DataGridView binding display.
The connection string is as follows:
The data Source IP address or the computed name (where the database is located), assuming that the local computer can be replaced with (local) or directly with the. ip:127.0.0.1.
Initial Catalog Database name
Integrated Security Connection (TRUE or false), if the Windows authentication method is true, if username and password login is false.
Username is provided when the item on the User ID is False
Password ...., provide Password for the ...., and the.
Therefore, the string is guaranteed in the urn for the above five items.
For example: The local sales database, username is sa,password to 123456, and the items are separated by semicolons, the string is as follows:
Data source= (Local); Initial catalog=sales;integrated Security=false; User Id=sa; password=123456;
Because it is locally used (local), it can also be replaced directly with the dot number, at the same time if the local computer name is Zheng, also available Zheng.
The following:
Create a sales database in SQlServer2012, and then build a table grade. The situation is as follows:
Open VS2012, create a window, add a DataGridView control, double-click the window to build the code:
Imports System.Data.SqlClientPublic Class Form1 Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As Dat ASet Private Sub form1_load (sender as Object, e as EventArgs) Handles mybase.load Dim cnstr as String = "Data sour ce= (local); Initial catalog=sales;integrated Security=false; User Id=sa; password=123456; " cn = New SqlConnection (cnstr) da = new SqlDataAdapter ("select * from grade", CN) ds = new DataSet () da. Fill (ds, "Grade") Datagridview1.datasource = ds. Tables ("Grade") End subend Class
The results of the execution are as follows:
This example simply remembers the "5" key value of the connection string when SQL Server is connected.
===================================================
Details:
Da. Fill (ds, "xxx")
Datagridview1.datasource = ds. Tables ("xxx")
The two sentences of xxx represent the table name, this table name and the original database table name can be different.
Since this is the table in the dataset result set, it is not a user-defined table name in the original database, so you can name it arbitrarily, but the two should be consistent.
In general, they do not define themselves, and if there is a special case, this self-defined situation can play a role.
vb.net database Programming (03): A simple example of a SQL Server connection query