VB. net access to the SQLServer Database

Source: Internet
Author: User
You can write a billing system in vb6.0 and use the ADODB (ActiveDataObjectsDataBase) Recordset. in vb.net, SqldataAdapter, SqldataReadr, Dataset, and Datatable are used. When you write the billing system for the second time, these vague concepts can also reflect the current impetuousness and lack of new knowledge acceptance ability. Below first

You can write a billing system in vb6.0 and use the ADODB (Active Data Objects Data Base) Recordset. in vb.net, you can use SqldataAdapter, SqldataReadr, Dataset, and Datatable, when I first wrote the charging system for the second time, these concepts were vague, which could also reflect the current impetuousness and lack of new knowledge acceptance ability. Below first

You can use vb6.0 to write a billing system. You can use ADODB (Active Data Objects Data Base) + Recordset, while SqldataAdapter, SqldataReadr, Dataset, and Datatable in vb.net, when I first wrote the charging system for the second time, these concepts were vague, which could also reflect the current impetuousness and lack of new knowledge acceptance ability. The following describes common methods and attributes.

SqlDataAdapter

If you only need to Execute SQL statements, you do not need to use DataAdapter. You can simply use the SQL command Execute series methods. SqlDataadapter serves as a bridge between Dataset and DataBase.

First, we will introduce common structures, methods, and attributes (System. Data. SqlClient must be referenced first)

Common Methods

Fill

The Fill method is used to populate the data in the database with dataset. For example:

Dim ds As New DataSet defines a dataset Dim da As New SqlDataAdapter defines a sqldataadapter 'to Fill in dataset da. Fill (ds, "User_info ")

Update

The Update method updates the data in dataset to the database. For example:

Dim ds As New DataSet defines a dataset Dim da As New SqlDataAdapter defines a sqldataadapter 'to Update dataset da. Update (ds, "User_info ")
DataSet

DataSet can be regarded as a database in memory, and DataSet is an independent data set independent of the database. The so-called independence means that, even if the data link is disconnected or the database is closed, DataSet is still available, and DataSet is composed of a group of able objects.

Dim ds As New DataSet defines a dataset ds. Tables. Add (dt) 'to Add datatabele to dataset.

DataTable

It is a virtual grid table that temporarily stores data and can be used to store data. For example, drag and drop a dview control on the form and write code in button1:

Dim dt As New DataTable () 'defines a datatabele dt. columns. add ("card number") 'Add the field "card number" dt. columns. add ("name") 'add field "name" dt. columns. add ("student ID") 'add the field "student ID" dt. columns. add ("instructor") 'Add the field "instructor" dt. rows. add ("10001", "jain", "0905024", "mi") 'write data rows to datatabele
Note that datatabele needs to add columns before adding rows. In my understanding, it is equivalent to adding fields in the database table before entering data. The effect is as follows:

SqldataReader

Sqldatareader is a method for obtaining database data. The obtained data is read-only. And it needs to be used with sqlCommand, the most common use method:

Dim ConnString As String = _ "Data Source = 192.168.24.158; Initial Catalog = CR_Charge_SYS; Persist Security Info = True; User ID = sa; password = 123456 "Dim sqltxt As String =" select * from User_info "'defines the SQL statement Dim sqlconn As New SqlConnection (ConnString)' and defines the connection Dim sqlcmd As New SqlCommand (sqltxt, sqlconn) 'define sqlCommand Dim userReader As SqlDataReader 'define sqldatareader Try sqlconn. open () 'Open the connection userReader = sqlcmd. executeReader 'generates sqldatareader userReader. read () 'read data MsgBox (userReader. item (3) 'displays the fourth read data userReader. close () 'Close read Catch ex As Exception MsgBox (ex. message) Finally sqlcmd. dispose () 'release sqlCommand End Try

In this way, the simplest Data Reading operation can be achieved. Note that to use sqldatareader, you must first open the database connection to generate sqldatareader, and you must. read () before obtaining data.

The following uses obtaining data from a table in the entire database as an example to use sqldatareader, sqldataadapter, datatabele, and dataset at the same time. Code:

Private Sub Button2_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles Button2.Click Dim ConnString As String = _ "Data Source = 192.168.24.158; Initial Catalog = CR_Charge_SYS; Persist Security Info = True; User ID = sa; password = 123456 "Dim sqltxt As String =" select * from User_info "'defines the SQL statement Dim sqlconn As New SqlConnection (ConnString)' and defines the connection Dim sqlcmd As New SqlCommand (sqltxt, sqlconn) 'define sqlCommand Dim da As New SqlDataAdapter (sqlcmd) 'define sqldataadapter and connect Dim ds As New DataSet Dim userReader As SqlDataReader' to define sqldatareader Try sqlconn. open () 'Open the connection userReader = sqlcmd. executeReader 'generates sqldatareader userReader. read () 'read data MsgBox (userReader. item (3) 'displays the fourth read data userReader. close () 'Close Reading da. fill (ds, "Userinfo") maid = ds. tables ("Userinfo") Catch ex As Exception MsgBox (ex. message) Finally sqlcmd. dispose () 'release sqlCommand End Try End Sub

Display result:



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.