快来帮忙!!!!!!急!!!!!!!

windeflower 2003-05-10 02:16:27
我现在正在毕业设计,我的应用程序连不上数据库
我不知道该怎么做,我想用ODBC,哪位大侠帮忙看看我的代码,指点一下,多谢!!!
我在线等待!!!Public UserName As String




'用ExecuteSQL函数执行SQL语句
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'传递参数SQL传递查询语句,Msgstring传递查询信息,自身以一个数据集对象形式返回

'定义连接
Dim cnn As ADODB.Connection
'定义数据集对象
Dim rst As ADODB.Recordset
'定义字符串
Dim sTokens() As String
'异常处理
On Error GoTo ExecuteSQL_error
'用Split函数产生一个包含各个子串的数组
sTokens = Split(SQL)
'创建连接
Set cnn = New ADODB.Connection
'打开连接
cnn.Open ConnectString
'判断字符串是否含有指定内容
If InStr("insert,delete,update", UCase$(sTokens(0))) Then
'执行查询信息
cnn.Execute SQL
MsgString = sTokens(0) & "执行成功"
Else
'创建数据集对象
Set rst = New ADODB.Recordset
'返回查询结果
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.movelast 'get recordcount

'返回记录集对象
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条记录"
End If


ExecuteSQL_exit:
'清空数据集对象
Set rst = Nothing
'中断连接
Set cnn = Nothing
Exit Function
'错误类型判断
ExecuteSQL_error:
MsgString = "查询错误:" & Err.Description
Resume ExecuteSQL_exit
End Function



'ConnectString函数用来连接数据库
Public Function ConnectString() As String
'返回一个数据连接
ConnectString = "driver={sql server};server=user5;UID=sa;PWa=;database=实验室"

End Function


'Testtxt函数检查各种文本框内容是否为空
Public Function Testtxt(txt As String) As Boolean
'判断输入内容是否为空
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function



'判断是否为授权用户
Sub Main()
Dim flogin As New frmLogin
'显示登录窗体
flogin.Show vbModal
'判断是否授权用户
If Not flogin.OK Then
'login faiked so exit app
End
End If
Unload flogin
'判断是将进入系统
Set fmainform = New MDIForm1
fmainform.Show
End Sub

...全文
33 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihonggen0 2003-05-10
  • 打赏
  • 举报
回复
可以访问

www.vbgood.com
www.dapha.net
www.21code.com
www.vbeden.com
www.myvc.net
msdn.microsoft.com
http://www.vbaccelerator.com/
http://www.mvps.org/vbnet/
http://www.freevbcode.com/
http://www.pscode.com/vb/
http://www.allapi.net
和MSDN得到更多的帮助.............
lihonggen0 2003-05-10
  • 打赏
  • 举报
回复
Public adoCN As New ADODB.Connection '定义数据库的连接存放数据和代码

Public SqlCommand As New ADODB.Command '定义 SQL 命令

Dim adoDateTime As New ADODB.Recordset '获取 NT-SERVER 时间

'***********************************************************************
'* 功能:与 SQL SERVER 数据库建立连接并取出服务器时间
'***********************************************************************
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
lihonggen0 2003-05-10
  • 打赏
  • 举报
回复
Dim CN As New adodb.Connection '定义数据库的连接存放数据和代码
Dim Rs As New adodb.Recordset
CN.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa;"
CN.Open
Rs.CursorLocation = adUseClient
Rs.Open "select * from employees", CN, adOpenDynamic
Set MSHFlexGrid1.DataSource = Rs

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧