gpt4 book ai didi

VB6 连接到数据库

转载 作者:行者123 更新时间:2023-12-01 22:37:05 25 4
gpt4 key购买 nike

好吧,我想做的事情听起来很简单。我想加载一个表单,并在表单加载时调用一个返回 1 项并将其放在 TextBox1 中的 sql 语句。这是我目前所拥有的。

 Private Sub Form_Load()

Call openTheDatabase

End Sub

Public Function openTheDatabase() As Boolean

'-- Here we want to open the database
Dim sConnectionString As String
Dim strSQLStmt As String

'-- Build the connection string
sConnectionString = "PROVIDER = MSDASQL;driver={SQL Server};database=databasename ;server=servername;uid=;pwd=;"


strSQLStmt = "SELECT chvDealerName " & _
"From dbo.tblDealers Where chrVSCAcctNum = '90442001'"

TextBox1.Text = strSQLStmt

End Function

编辑

好的,它是 Text1.Text,但现在我只是获取文本框中的字符串,而不是实际的数据库条目

最佳答案

您可以在此处查看设置 ADODB 连接的可靠示例:http://www.timesheetsmts.com/adotutorial.htm

您的项目首先需要引用 ADODB 库。为此:

  1. 打开你的项目
  2. 点击顶部的项目菜单,然后点击下拉列表中的引用
  3. 检查“Microsoft ActiveX 数据对象 2.x 库”(x 是您看到的最大数字 - 在我的 Windows XP Pro SP2 盒子上,它是 2.7"

您的项目示例:

Private Sub Form_Load()

Call openTheDatabase

End Sub

Public Function openTheDatabase() As Boolean

'-- Here we want to open the database
Dim sConnectionString As String
Dim strSQLStmt As String

'-- Build the connection string
sConnectionString = "PROVIDER = MSDASQL;driver={SQL Server};database=databasename ;server=servername;uid=;pwd=;"


strSQLStmt = "SELECT chvDealerName " & _
"From dbo.tblDealers Where chrVSCAcctNum = '90442001'"

'DB WORK
Dim db As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim result As String

db.ConnectionString = sConnectionString
db.Open 'open connection

With cmd
.ActiveConnection = db
.CommandText = strSQLStmt
.CommandType = adCmdText
End With

With rs
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmd
End With

If rs.EOF = False Then
rs.MoveFirst
Let result = rs.Fields(0)
End If
'close conns
rs.Close
db.Close
Set db = Nothing
Set cmd = Nothing
Set rs = Nothing


'set local box

' TextBox1.Text = strSQLStmt
TextBox1.Text = result


End Function

关于VB6 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21411315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com