标题:新手小白 求教连接数据库问题
只看楼主
pei791128
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-10-15
 问题点数:0 回复次数:1 
新手小白 求教连接数据库问题
想连接 MS SQL 数据库,然后返回一个列表,用datagrid控件,有数据,但是用DATACOMBO控件就没数据 应该怎么写代码
Private Sub dataCombo1_GotFocus()
dim conn as adodb.connection
dim rst as adodb.recordset
Set Conn = New ADODB.Connection '数据库对象的实例化
Set Rst = New ADODB.Recordset '记录集对象的实例化
Conn.CursorLocation = adUseClient
Conn.Open "Provider=SQLOLEDB.1;Password=Pei%791128;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=192.168.11.252"
'执行 ADODB.Recordset对象的Open方法,获得记录集
Rst.Open "Select cacc_name From t_kdaccount_gl", Conn, adOpenKeyset, adLockPessimistic
'将返回的记录集显示到Datacombo1
Set dataCombo1.DataSource = Rst
Set Rst = Nothing '释放记录集对象
Set Conn = Nothing '释放数据库对
搜索更多相关主题的帖子: 记录集 adodb conn 对象 Set 
2019-10-15 17:11
pei791128
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-10-15
得分:0 
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCon As String
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset

strCon = "Provider=SQLOLEDB.1;Password=Pei%791128;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=127.0.0.7" '填写连接字符串,可选将 Data Source=127.0.0.1 换进去
conn.ConnectionString = strCon
conn.CursorLocation = adUseClient
conn.Open '数据库打开成功
Dim SQL As String
SQL = "Select cacc_name  From t_kdaccount_gl" '填写SQL查询命令
rs.Open SQL, conn, 1, 1
Set DataCombo1.DataSource = rs
DataCombo1.DataField = "cacc_name"
Set DataCombo1.RowSource = rs
DataCombo1.ListField = "cacc_name"
2019-10-15 22:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-497286-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.631358 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved