rivate Sub cmdOK_Click()
'注册新用户Dim cnn1 As ADODB.Connection
Dim AddUser As New ADODB.Recordset
Dim SqlStr As String
Dim DBstr As String
Dim UsrName As String
Dim UsrPwd As String
Dim strCnn As String '与数据库连接
Set cnn1 = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = localhost ;User ID = sa; & _
Initial catalog = wastebook ;Password = ;"
cnn1.Open strCnn '与表连接
Set AddUser = New ADODB.Recordset
AddUser.CursorType = adOpenKeyset
AddUser.LockType = adLockOptimistic
×× AddUser.Open "UserInfo", cnn1, adOpenForwardOnly, adLockOptimistic
End Sub
以上代码通过,于是我新建了一个模块,并Public g_Conn As ADODB.Connection 把g_Conn设为全局变量,在模块中
'建立连接
Set g_Conn = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
而把上面与数据库的连接代码去掉,运行就会报错“连接无法用于执行此操作。在此上下文中它可能已经被关闭或无效。错误的是带××的那行,请大家帮忙,谢谢:)
'注册新用户Dim cnn1 As ADODB.Connection
Dim AddUser As New ADODB.Recordset
Dim SqlStr As String
Dim DBstr As String
Dim UsrName As String
Dim UsrPwd As String
Dim strCnn As String '与数据库连接
Set cnn1 = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = localhost ;User ID = sa; & _
Initial catalog = wastebook ;Password = ;"
cnn1.Open strCnn '与表连接
Set AddUser = New ADODB.Recordset
AddUser.CursorType = adOpenKeyset
AddUser.LockType = adLockOptimistic
×× AddUser.Open "UserInfo", cnn1, adOpenForwardOnly, adLockOptimistic
End Sub
以上代码通过,于是我新建了一个模块,并Public g_Conn As ADODB.Connection 把g_Conn设为全局变量,在模块中
'建立连接
Set g_Conn = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
而把上面与数据库的连接代码去掉,运行就会报错“连接无法用于执行此操作。在此上下文中它可能已经被关闭或无效。错误的是带××的那行,请大家帮忙,谢谢:)
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
放在那里都不应该错呀
AddUser.Open "UserInfo", g_Conn, adOpenForwardOnly, adLockOptimistic
这句我也有的啊,不过错误好像就是在这一行
Set g_Conn = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
要放在一个函数里面?直接放在模块里面可以吗?我对模块的概念还不是最清楚
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
Dim g_Conn As ADODB.Connection '把Public声明改用Dim试试看
Sub Main()
Set g_Conn = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
End Sub'----下面是在Form中使用
Private Sub cmdOK_Click()
'注册新用户Dim AddUser As New ADODB.Recordset
Dim SqlStr As String
Dim DBstr As String
Dim UsrName As String
Dim UsrPwd As String
Dim strCnn As String
'与表连接
Set AddUser = New ADODB.Recordset
AddUser.CursorType = adOpenKeyset
AddUser.LockType = adLockOptimistic
AddUser.Open "UserInfo", strCnn, adOpenForwardOnly, adLockOptimistic
End Sub
End Sub第二个参数为strCnn?
你的Connection对象名是g_Conn
AddUser.Open "UserInfo", g_Conn, adOpenForwardOnly, adLockOptimistic
strCnn是一个string,用来存储与数据库的连接方式,用户名,密码等信息
而且strCnn并不是全局的,只在模块中定义过,那FORM中怎么使用它呢?
今天才接触ADO,来了个下马威
Dim g_Conn As ADODB.ConnectionSub Main()
Set g_Conn = New ADODB.Connection
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
End Sub然后修改工程属性,将开始对象设定为 Main()
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
........ServerName, UserName, DBName, UserPwd ??
用这句
strCnn = "Provider = SQLOLEDB; Data Source = localhost ;User ID = sa; & _
Initial catalog = wastebook ;Password = ;"
Dim strCnn As String'数据库相关信息
ServerName = "localhost"
DBName = "wastebook"
UserName = "sa"
UserPwd = " "
这是我定义的。
用你前面的办法试了一下,改成从sub main开始运行,不过VB就没有响应了,我导出成exe文件,运行等了一会儿然后跳出来错误信息“运行时错误,【DBNETLIB】connectionOpen(connect())】SQL Server不存在或拒绝访问
好像还是不行
'Set g_Conn = New ADODB.Connection '不要这句
strCnn = "Provider = SQLOLEDB; Data Source = ServerName ;User ID = UserName;" & _
"Initial catalog = DBName ;Password = UserPwd; "
g_Conn.Open strCnn
是不是没有关闭连接
以后的连接就出错了