vb language using ADO to connect and manipulate SQL Server database tutorial
This article mainly introduces the VB language using ADO connection, operation SQL Server database tutorial, this article explains in detail, the code has a lot of comments, is a very good tutorial, the need for friends can refer to the next
A few years ago learned VB almost forget, these days reviewed the next. VB connection to ADO database is not very difficult.
Connect the first step (to look closely)
For small white, here is the most detailed tutorial, the first step to connect ADO database, what parts to add? The full name is the Microsoft ADO Data Control 6.0 (SP6) (OLE DB) part.
In the Microsoft ADO Data Control 6.0 (SP6) (OLE DB) part, there is a name: ADODC Data Control that you want to add. Locate the acces in the ADODC Data Control data location.
The method referenced by the control (the value refers to the name)
The code is 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 greatly, in the VB group write this thing, let me borrow, and then I found a lot of errors, y God modified several times after the error, simply rewrite it again, this is the code later. The time passed quickly, the VB Group also dissolved. Later, for some reason, many tutorials now use this code.
The second part is the code connection of AOD, because the second part relates to the Recordset object and the Connection object, you can learn it by yourself and write it yourself completely.
What is the Recordset object and the Connection object, the connection object is the connection to the data source, and the Recordset object is the operational data.
Looking for Microsoft ADO Data Control 6.0 (SP6) (OLE DB) parts
VB connects SQL Server database with ADO
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
‘数据源信息常量
Public Const conn
As String =
"Provider = SQLOLEDB.1;Password = sa; UserID = sa; Initial Catalog = StudentFiles; Data Source = localhost"
Public Const CONNECT_LOOP_MAX = 10
‘一次执行connect操作,可以访问数据库的次数
Private IsConnect
As Boolean ‘标记数据库是否连接
Private Connect_Num
As Integer ‘标记执行Connect()函数后访问数据的次数
Private cnn
As ADDODB.Connection
‘连接数据库的Connect对象
Private re
As ADDODB.Recordset
‘保存结果集的Recordset对象
//连接数据库
Private Sub Connect()
‘如果连接标记为真,则返回。
IF IsConnect =
True Then
Exit Sub
End If
Set cnn =
New ADODB.Connection
‘关键new用于创建新对象cnn
cnn.ConnectionString = conn
cnn.Open
‘判断连接的状态
If cnn.State <> adStateOpen
Then
MsgBox
"数据库连接失败"
End
End If
‘设置连接标识,表示已经连接到数据库
IsConnect =
True
End Sub
‘断开与数据库的连接
Private Sub DisConnect()
Dim rc
As Long
If IsConnect =
False Then
Exit Sub
End If
‘关闭连接
cnn.Close
‘释放cnn
Set cnn =
Nothing
IsConnect =
False
End Sub
‘使用Connect_Num控制数据连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
Connect
End Sub
‘使用Connect_Num控制数据断开
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX
Then
Connect_Num = 0
Disconnect
End If
End Sub
‘强制关闭api方式访问俄的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
Disconnect
End Sub
‘执行数据库操作语言
‘byval 就是按参数的值传递,再传递过程中,参数不会发生变化(也就是将参数值而不是将地址传递给过程的方式,这就使过程访问发哦变量的副本,过程不可改变变量的值);
与之对应的是byref,指按参数的地址传值,byref可以省略
Public Sub SQLExt(
ByVal TmpSQLstmt
As String )
Dim cmd
As New ADODB.Command
‘创建Command对象cmd
DB_Connect
‘连接数据库
Set cmd.ActiveConnection = cnn
‘设置cmd的ActiveConnect属性,指定与其关联的数据库连接
cmd.CommandText = TmpSQLstmt
‘设置要执行的命令文本
‘MsgBox TmpSQLstmt
cmd.Execute
‘执行命令
Set cmd =
Nothing
DB_DisConnect
‘断开与数据库的连接
End Sub
‘执行数据库查询语句
Public Function QueryExt(
ByVal TmpSQLstmt
As String )
As ADODB.Recordset
Dim rst
As New ADODB.Recordset
‘创建Rescordset对象rst
DB_Connect
‘连接数据库
Set rst.ActiveConnection = cnn
‘设置rst的ActiveConnection属性,指定与其相关的数据库的连接
rst.CursorType = adOpenDynamic
‘设置游标类型
rst.LockType = adLockOptimistic
‘设置锁定类型
rst.Open TmpSQLstmt
‘打开记录集
Set QueryExt = rst
‘返回记录集
End Function
|
vb language using ADO to connect and manipulate SQL Server database tutorial