VB language use ADO to connect, operate SQL Server database tutorial _vb

Source: Internet
Author: User
Tags ole

VB learned a few years ago almost forget the light, these days to review the next. VB connection to the ADO database is not very difficult.

Connect the first step (to look carefully)

For small white speaking, here's the most detailed tutorial, the first step to connect to the ADO database, what parts to add? The full name is the Microsoft ADO Data Control 6.0 (SP6) (OLE DB) component.

In the Microsoft ADO Data Control 6.0 (SP6) (OLE DB) widget, there is a name: ADODC, to add it. Locate the acces in the ADODC Data Control data location.

The method referenced by the control (the value refers to the name)

Copy Code code as follows:

For i = 1 to Adodc1.Recordset.RecordCount
If not Adodc1.Recordset.EOF Then

Combo1.additem Adodc1.Recordset.Fields ("value"). Value

Adodc1.Recordset.Movenext
End If
Next I


The code refers to the writer is ydl890406 large, in the VB group to write this thing, let me borrow, and then I found that there are many errors, y God modified several times after the error, and simply rewrite, this is the later code. The time passed quickly, the VB Group also dissolved. Later, for some reason, many tutorials now use this code.

The second part is the AOD code connection, because the second part involves the Recordset object and the Connection object, oneself learns, can write oneself completely.

What is a Recordset object and a Connection object, the connection object is a connection to the data source, and the Recordset object is the operational data.

Looking for a Microsoft ADO Data Control 6.0 (SP6) (OLE DB) component

VB connects SQL Server database with ADO

 ' Data source information constant Public Const conn as String = ' Provider = sqloledb.1; Password = sa; UserID = sa; Initial Catalog = studentfiles; Data Source = localhost "public Const Connect_loop_max = 10" Once CONNECT operation, the number of times you can access the database Private isconnect as Boolean ' tag Whether the database is connected to the private connect_num as Integer token to access the data after performing the Connect () function private CNN as ADDODB. Connection ' connects the database to the Connect object Private re as addodb.
 Recordset ' Saves the Recordset object//Connection database Private Sub connect () ' of the result set ' If the connection is marked True, return. IF isconnect = True Then Exit Sub End If Set cnn = New ADODB. Connection ' key new is used to create the next object CNN CNN. ConnectionString = conn CNN. Open ' determines the status of the connection If CNN. State <> adStateOpen Then MsgBox "database connection Failed" End If ' Sets the connection identity, indicating that the database has been connected to Isconnect = True End Sub ' disconnected from the database Connection Private Sub DisConnect () Dim RC as Long If isconnect = False Then Exit Sub end If ' Close connection CNN. Close ' Release CNN Set cnn = Nothing isconnect = False End Sub ' uses Connect_num control data connection public Sub db_connect () Connect_num = Connect_num + 1 Connect End Sub ' use Connect_num to control Data disconnect public Sub db_disconnect () If connect_num >= Connect_loop_max Then = 0 Dis Connect End If End Sub ' force off API way to access Russian database, counter reset public Sub dbapi_disconnect () connect_num = 0 Disconnect End Sub ' hold Row database Operation Language ' ByVal is passed by the value of the parameter, in the transfer process, the parameter will not change (that is, the parameter value instead of passing the address to the process of the way, which allows the process to access the copy of the variable, the process can not change the value of the variable); the corresponding is byref, the value of the address by the parameter , byref can omit the public Sub sqlext (ByVal tmpsqlstmt as String) Dim cmd as New ADODB. Command ' Create command object cmd db_connect ' to connect to database Set cmd. ActiveConnection = CNN ' Sets cmd's Activeconnect property, specifying its associated database connection to CMD. CommandText = tmpsqlstmt ' Sets the command text to execute ' MsgBox tmpsqlstmt cmd. Execute ' execute command Set cmd = Nothing db_disconnect ' Disconnect the connection end Sub ' with the database execute the database query statement public Function queryext (ByVal tmpsqls TMT as String) as ADODB. Recordset Dim rst as New ADODB. The Recordset ' creates Rescordset object rst Db_connect ' connects to the database Set rst. ActiveConnection = CNN ' Sets the ActiveConnection property of RST to specify a connection rst to the database associated with it. CursorType = adOpenDynamic ' Sets the cursor type RST. LockType =adLockOptimistic ' Sets the lock type RST.

 Open tmpsqlstmt ' Turn recordset Set queryext = Rst ' to return recordset end Function
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.