怎样用代码建立数据库连接?

pcbaby 2002-05-20 03:40:04
Private Sub Form_Activate()
Data1.DatabaseName = "kxqa.mdb"
Data1.Connect = ";pwd=" & 888
Data1.RecordSource = "题库"
q6.DataSource = Data1
q6.DataField = "备选1"
End Sub

q6.DataSource = Data1
q6.DataField = "备选1"
运行到这两行时,总是datasource变反白,提示未找到方法或数据成员
...全文
19 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pcbaby 2002-05-20
可是我用的data1控件连上数据库了,文本框的数据源不也是data1吗?
q6.DataSource = Data1
哪里不对了?怎么一到这就出错?
回复
lihonggen0 2002-05-20
数据源不对
回复
pcbaby 2002-05-20
现在问题就是我前两句数据库是连上了,但后面的文本框和数据库却连不上
回复
lihonggen0 2002-05-20
Public adoCN As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNAccess As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNAccess1 As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNtemp As New ADODB.Connection '临时数据库
Public SqlCommand As New ADODB.Command '定义 SQL 命令
Public RsUsers As New ADODB.Recordset
Public RsDept As New ADODB.Recordset
Public Rs_Dm_Level As New ADODB.Recordset
Dim adoDateTime As New ADODB.Recordset '获取 NT-SERVER 时间
'***********************************************************************
'* 功能:与 SQL SERVER 数据库建立连接并取出服务器时间
'***********************************************************************
Public Function OpenConnection1() As String '打开数据库
End Function

Public Function OpenConnection() As String '打开数据库
On Error GoTo SQLConErr
With adoCN
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
.ConnectionTimeout = 15
.Open

If .State = adStateOpen Then
adoDateTime.Open "select getdate()", adoCN, adOpenStatic, adLockOptimistic
cServerDate = Format(adoDateTime(0), "yyyy-mm-dd")
cServertime = Mid(adoDateTime(0), 10)
Else
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End With

SqlCommand.ActiveConnection = adoCN
SqlCommand.CommandType = adCmdText
Exit Function
SQLConErr:
Select Case Err.Number
Case -2147467259
MsgBox "找不到指定的SQL Server服务器或者数据库不存在,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case -2147217843
MsgBox "指定的SQL Server数据库用户不存在或口令错误,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End Select
OpenConnection
End Function

'***********************************************************************
'* 功能:连接数据报表环境
'*
'***********************************************************************
Public Sub OpenDEConnection() '连接数据环境
On Error GoTo DEConErr
With DE_Report.Con_report
If .State = adStateOpen Then
.Close
End If
.CursorLocation = adUseClient
.ConnectionTimeout = 15
.ConnectionString = "Provider=SQLOLEDB.1;Password=" & cSQLPassword & ";Persist Security Info=True;User ID=" & cSQLUserName & ";Initial Catalog=" & cDatabaseName & ";Data Source=" & cNtServerName
.Open
End With
Exit Sub
DEConErr:
Select Case Err.Number
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End Select
End Sub

Public Function OpenAccess() As String
With adoCNAccess
If .State <> adStateOpen Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & cProgramPath & "Trade.mdb" ';password=allway"
.ConnectionTimeout = 5
.Open
If .State = adStateOpen Then
OpenAccess = "数据库连接成功"
Else
OpenAccess = "数据库连接失败,请按帮助进行检查 !"
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End If
End With
End Function

回复
lihonggen0 2002-05-20
Public adoCN As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNAccess As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNAccess1 As New ADODB.Connection '定义数据库的连接存放数据和代码
Public adoCNtemp As New ADODB.Connection '临时数据库
Public SqlCommand As New ADODB.Command '定义 SQL 命令
Public RsUsers As New ADODB.Recordset
Public RsDept As New ADODB.Recordset
Public Rs_Dm_Level As New ADODB.Recordset
Dim adoDateTime As New ADODB.Recordset '获取 NT-SERVER 时间
'***********************************************************************
'* 功能:与 SQL SERVER 数据库建立连接并取出服务器时间
'***********************************************************************
Public Function OpenConnection1() As String '打开数据库
End Function

Public Function OpenConnection() As String '打开数据库
On Error GoTo SQLConErr
With adoCN
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
.ConnectionTimeout = 15
.Open

If .State = adStateOpen Then
adoDateTime.Open "select getdate()", adoCN, adOpenStatic, adLockOptimistic
cServerDate = Format(adoDateTime(0), "yyyy-mm-dd")
cServertime = Mid(adoDateTime(0), 10)
Else
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End With

SqlCommand.ActiveConnection = adoCN
SqlCommand.CommandType = adCmdText
Exit Function
SQLConErr:
Select Case Err.Number
Case -2147467259
MsgBox "找不到指定的SQL Server服务器或者数据库不存在,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case -2147217843
MsgBox "指定的SQL Server数据库用户不存在或口令错误,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End Select
OpenConnection
End Function

'***********************************************************************
'* 功能:连接数据报表环境
'*
'***********************************************************************
Public Sub OpenDEConnection() '连接数据环境
On Error GoTo DEConErr
With DE_Report.Con_report
If .State = adStateOpen Then
.Close
End If
.CursorLocation = adUseClient
.ConnectionTimeout = 15
.ConnectionString = "Provider=SQLOLEDB.1;Password=" & cSQLPassword & ";Persist Security Info=True;User ID=" & cSQLUserName & ";Initial Catalog=" & cDatabaseName & ";Data Source=" & cNtServerName
.Open
End With
Exit Sub
DEConErr:
Select Case Err.Number
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End Select
End Sub

Public Function OpenAccess() As String
With adoCNAccess
If .State <> adStateOpen Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & cProgramPath & "Trade.mdb" ';password=allway"
.ConnectionTimeout = 5
.Open
If .State = adStateOpen Then
OpenAccess = "数据库连接成功"
Else
OpenAccess = "数据库连接失败,请按帮助进行检查 !"
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End If
End With
End Function

回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告