VB.net数据库编程(03):一个SQLserver连接查询的简单例子
这个例子,由于在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.情况如下:
打开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
运行结果如下:
本例只须记住SQlserver连接时,连接字串的“5个”关键值就行了。
===================================================
细节:
da.Fill(ds, "xxx")
DataGridView1.DataSource = ds.Tables("xxx")
这两句的xxx表示表名, 这个表名与原数据库的表名可以不一样。
因为这里是DataSet结果集中的表了,不是原数据库中的,用户自己定义的表名,因此可以任意命名,但这两名应保持一致。
一般都不会自己 再定义,如果有特殊情况时,这个自定义情况就可发挥作用。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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())

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.

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.

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

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

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