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, 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.

Related Article

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.