VB.net database programming (03): a simple example of SQL Server connection Query

Source: Internet
Author: User
In this example, we will review it again because we have already learned it at the beginning of ADO.net. Master the connection string. The process is: 1. Reference System. Data. SqlClient. Access references System. Data. OleDB. So there are differences. 2. Related connections, adapters, and datasets. 3. The DataGridView binding is displayed. Connection

In this example, we will review it again because we have already learned it at the beginning of ADO.net. Master the connection string. The process is: 1. Reference System. Data. SqlClient. Access references System. Data. OleDB. So there are differences. 2. Related connections, adapters, and datasets. 3. The DataGridView binding is displayed. Connection

In this example, we will review it again because we have already learned it at the beginning of ADO.net.


Master the connection string.

The process is:

1. Reference System. Data. SqlClient. Access references System. Data. OleDB, which is different.

2. Related connections, adapters, and datasets.

3. The DataGridView binding is displayed.


The connection string is as follows:

Data Source IP address or computing name (database location). If it is a local computer, it can be replaced by (local) or directly by., or the local IP address is 127.0.0.1.

Initial Catalog Database Name

Integrated Security connection (true or false), true for Windows authentication, false for username and password logon.

Provide the User name when the item on the User ID is false.

Password ...... provide the Password.


Therefore, strings are guaranteed in the preceding five items.

For example, in the local Sales database, the user name is sa and the password is 123456. Separate the items with semicolons. The strings are 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 by a dot. If the Local computer is named Zheng, Zheng can also be used.


The following is the question:

Create a Sales database in SQlServer2012 and create a table grade in it as follows:




Open VS2012, create a form, add a dview control, and double-click the form to create the Code:

Imports System.Data.SqlClientPublic Class Form1    Dim cn As SqlConnection    Dim da As SqlDataAdapter    Dim ds As DataSet    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        Dim cnStr As String = "Data Source=(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 running result is as follows:



In this example, you only need to remember the "5" key values of the connection string during SQlserver connection.



========================================================== ==============


Details:

Da. Fill (ds, "xxx ")
DataGridView1.DataSource = ds. Tables ("xxx ")

The xxx in these two statements indicates the table name, which may be different from the table name in the original database.

This is because the tables in the DataSet result set are not in the original database. The user-defined table names can be named at will, but these two names should be consistent.


Generally, it is not defined by itself. In special cases, this custom situation can play a role.

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.