问下VB连sql问题
PublicSqlConnAsADODB.ConnectionPublicsConnAsStringPublicSubOpenConn()IfSqlConnIsNothi...
Public SqlConn As ADODB.ConnectionPublic sConn As String
Public Sub OpenConn() If SqlConn Is Nothing = True Then '建立数据库连接对象 Set SqlConn = New ADODB.Connection End If If SqlConn.State <> 1 Then '打开数据库连接 SqlConn.Open sConn End IfEnd Sub
Public Sub CloseConn() '如果数据库连接对象不为空则关闭数据库连接 If SqlConn.State = 1 Then SqlConn.Close End IfEnd Sub
Public Function RunSelectSQL(ByVal sSQLString As String) As ADODB.Recordset Dim rs As New Recordset '打开数据库连接 Me.OpenConn '执行SQL操作 rs.Open sSQLString, SqlConn, adOpenStatic, adLockReadOnly Set RunSelectSQL = rsEnd Function
Public Function RunSelectSQLUpdatable(ByVal sSQLString As String) As ADODB.Recordset Dim rs As New Recordset '打开数据库连接 Me.OpenConn '执行SQL操作 rs.Open sSQLString, SqlConn, adOpenStatic, adLockBatchOptimistic Set RunSelectSQLUpdatable = rsEnd Function
Private Sub Class_Initialize() sConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jxclink"End Sub
怎么运行一直说截图的问题?谢谢 展开
Public Sub OpenConn() If SqlConn Is Nothing = True Then '建立数据库连接对象 Set SqlConn = New ADODB.Connection End If If SqlConn.State <> 1 Then '打开数据库连接 SqlConn.Open sConn End IfEnd Sub
Public Sub CloseConn() '如果数据库连接对象不为空则关闭数据库连接 If SqlConn.State = 1 Then SqlConn.Close End IfEnd Sub
Public Function RunSelectSQL(ByVal sSQLString As String) As ADODB.Recordset Dim rs As New Recordset '打开数据库连接 Me.OpenConn '执行SQL操作 rs.Open sSQLString, SqlConn, adOpenStatic, adLockReadOnly Set RunSelectSQL = rsEnd Function
Public Function RunSelectSQLUpdatable(ByVal sSQLString As String) As ADODB.Recordset Dim rs As New Recordset '打开数据库连接 Me.OpenConn '执行SQL操作 rs.Open sSQLString, SqlConn, adOpenStatic, adLockBatchOptimistic Set RunSelectSQLUpdatable = rsEnd Function
Private Sub Class_Initialize() sConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jxclink"End Sub
怎么运行一直说截图的问题?谢谢 展开
1个回答
展开全部
ADO 的方式访问SQL 数据库的字符串如下:
Conn="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa; Password=xxx; Data Source =服务器名;Initial Catalog=数据库名;"
似乎你的连接字符串有些问题!
Conn="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa; Password=xxx; Data Source =服务器名;Initial Catalog=数据库名;"
似乎你的连接字符串有些问题!
追问
请问下这段代码要加到我程序的哪里呢
追答
建立SQL数据库连接如下:
dim adoConn as New ADODB.Connection
dim Conn as String
Conn="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa; Password=xxx; Data Source =服务器名;Initial Catalog=数据库名;"
adoConn.Open Conn '建立连接
也许是你的程序看不全面,我觉得好象是用一个类建立连接的,但程序不全,所以不好分析.
你先把程序中
Private Sub Class_Initialize()
sConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jxclink"
End Sub
里面的 sConn 替换一下,调试看一下如何?
sConn="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa; Password=xxx; Data Source =服务器名;Initial Catalog=数据库名;"
注意:SQL 数据库中 sa 的密码,数据库名称及服务器名称几项要写正确.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询