Home Database Mysql Tutorial VB.net数据库编程(03):一个SQLserver连接查询的简单例子

VB.net数据库编程(03):一个SQLserver连接查询的简单例子

Jun 07, 2016 pm 03:45 PM
sqlserver database programming connect

这个例子,由于在ADO.net入门已经专门学了,再次进行复习 一下。 主要掌握连接字串的情况。 过程就是: 1、引用System.Data.SqlClient。而Access中引用 的是System.Data.OleDB.所以是有区别的 2、相关连接、适配器、数据集。 3、DataGridView绑定显示。 连接

这个例子,由于在ADO.net入门已经专门学了,再次进行复习 一下。


主要掌握连接字串的情况。

过程就是:

1、引用System.Data.SqlClient。而Access中引用 的是System.Data.OleDB.所以是有区别的

2、相关连接、适配器、数据集。

3、DataGridView绑定显示。


连接字串如下:

Data Source                     IP地址或计算名(数据库所在位置的),如果是本地计算机可以用(local)或直接用.来代替,或者本地IP:127.0.0.1代替。

Initial  Catalog                  数据库名

Integrated Security      安全连接情况(真或假),若是Windows验证方式则为真,若是用户名及密码登录则为假。

User ID                         上项为假时,提供用户名

Password                     ...................,提供密码。


因此,就上面五项就瓮中保证了字串。

如:本地上Sales数据库,用户名为sa,密码为123456,各项之间用分号隔开,则字串为如下:

Data Source=(Local);Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;

因为在本地所以用了(Local),还可直接用点号来代替,同时若本地计算机名为Zheng,还可用Zheng。


下面正题:

在SQlServer2012中建立一个Sales数据库,再在其中建立一个表grade.情况如下:

VB.net数据库编程(03):一个SQLserver连接查询的简单例子



打开VS2012,建立一个窗体,添加一DataGridView控件,双击窗体建立代码:

Imports System.Data.SqlClient

Public 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 Sub
End Class
Copy after login

运行结果如下:

VB.net数据库编程(03):一个SQLserver连接查询的简单例子


本例只须记住SQlserver连接时,连接字串的“5个”关键值就行了。



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


细节:

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

这两句的xxx表示表名,  这个表名与原数据库的表名可以不一样。

因为这里是DataSet结果集中的表了,不是原数据库中的,用户自己定义的表名,因此可以任意命名,但这两名应保持一致。


一般都不会自己 再定义,如果有特殊情况时,这个自定义情况就可发挥作用。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

See all articles