VB6连接数据库(VB6 Connecting to a Database)

编程入门 行业动态 更新时间:2024-10-27 02:25:27
VB6连接数据库(VB6 Connecting to a Database)

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

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,但现在我只是在文本框中获取字符串,而不是实际的数据库条目

Alright, what i want to do sounds simple. I would like to load a form and on form load call a sql statement that returns 1 item and places it in TextBox1. Here is what i have so far.

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

EDIT

Ok it was Text1.Text, but now im just getting the string in the textbox, not the actual database entry

最满意答案

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

您的项目需要先引用ADODB库。 去做这个:

打开你的项目 单击顶部的“ 项目”菜单,然后单击下拉列表中的“ 引用 ” 检查“Microsoft ActiveX Data Objects 2.x Library” (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

You can see a solid example of setting up ADODB connections here: http://www.timesheetsmts.com/adotutorial.htm

Your project needs a reference to the ADODB library before anything. To do this:

Open your project Click the Project menu on the top and click References in the dropdown Check the "Microsoft ActiveX Data Objects 2.x Library" (with x being the highest number you see - on my Windows XP Pro SP2 box, it is 2.7"

Example with your project:

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

更多推荐

本文发布于:2023-07-22 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接数据库   Database   Connecting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!