没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
VB 网络版系统开发——远程数据库连接
一、PublicVar 模块:
Public LoginSucceeded As Boolean '用户登录标志
Public UserID As String '用户 ID
Public UserStyle As String '保存用户类型
Public ConnectionString As String '数据库连接字符串
二、OPeratDataBase 模块:
Public Function GetConnStr() As String
'得到数据库连接字符串,用户可以在此处设置连接字符串
'User ID 是数据库用户 ID,Password 是登陆密码
'Initial Catalog 是数据库名,Data Source 是服务器名称
GetConnStr = ConnectionString
End Function
Public Function OpenConn(ByRef Conn As ADODB.Connection) As Boolean
'打开数据库连接,连接成功返回 true,出错时返回 false
Set Conn = New ADODB.Connection
'出错处理
On Error GoTo ErrorHandle
Conn.Open GetConnStr
OpenConn = True
Exit Function
ErrorHandle:
MsgBox "连接数据库失败!请重新连接!"
OpenConn = False
Exit Function
End Function
Public Sub ExecuteSQL(ByVal SQL As String, ByRef msg As String)
'执行 SQL 语句
Dim Conn As ADODB.Connection
Dim sTokens() As String
'出错处理
On Error GoTo ErrorHandle
'判断 SQL 语句
sTokens = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", UCase((sTokens(0)))) Then
'打开数据库连接
If OpenConn(Conn) Then
Conn.Execute SQL
msg = sTokens(0) & "操作执行成功!"
End If
Else
msg = "SQL 语句有误:" & SQL
End If
1
Finally_Exit:
Set rst = Nothing
Set Conn = Nothing
Exit Sub
ErrorHandle:
msg = "执行错误: " & Err.Description
Resume Finally_Exit
End Sub
Public Function SelectSQL(ByVal SQL _
As String, ByRef msg As String) As ADODB.Recordset
'执行 SQL 语句,返回 ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
'出错处理
On Error GoTo ErrorHandle
'判断 SQL 语句
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
'打开数据库连接
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set SelectSQL = rst
msg = "查询到" & rst.RecordCount & " 条记录! "
End If
Else
msg = "SQL 语句有误:" & SQL
End If
Finally_Exit:
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle:
MsgString = "查询错误: " & _
Err.Description
Resume Finally_Exit
End Function
Public Function BatchSelectSQL(ByVal SQL _
As String, ByRef msg As String) As ADODB.Recordset
'执行 SQL 语句,返回批处理的 ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
2
Dim sTokens() As String
'出错处理
On Error GoTo ErrorHandle
'判断 SQL 语句
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
'打开数据库连接
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockBatchOptimistic
Set BatchSelectSQL = rst
msg = "查询到" & rst.RecordCount & " 条记录! "
End If
Else
msg = "SQL 语句有误:" & SQL
End If
Finally_Exit:
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle:
MsgString = "批查询错误: " & _
Err.Description
Resume Finally_Exit
End Function
三、数据库连接窗口(F 数据库连接):
Private Sub CmdOK_Click()
'确定操作
ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;"
ConnectionString = ConnectionString & "User ID=" & Trim(txtUserId.Text) &
";Password=" & Trim(txtPwd.Text) & ";"
ConnectionString = ConnectionString & "Initial Catalog=" & Trim(txtDatabase.Text) &
";Data Source=" & Trim(txtServer.Text) & ""
E 重新登录.Show
Unload Me
End Sub
四、登入窗口(E 重新登录):
Option Explicit
Dim SQL As String
Dim msg As String
Dim Index As Integer
Private Sub Form_Load()
'初始化用户类型
3
Dim list As ListItem
Set list = ListView1.ListItems.Add(, , "考生", 3)
Set list = ListView1.ListItems.Add(, , "教师", 1)
Set list = ListView1.ListItems.Add(, , "工作人员", 2)
UserStyle = "考生" '默认用户类型为考生
End Sub
Private Sub ListView1_Click()
'选择用户类型
UserStyle = ListView1.SelectedItem.Text '保存用户类型
txtuser.Text = "000001" '默认的用户 ID,三种类型都一样,为了方便测试和使用
End Sub
Private Sub CmdOK_Click()
'确定操作
Dim rst As ADODB.Recordset
Dim password As String '保存密码
UserID = Trim(txtuser.Text)
password = Trim(txtPassword.Text)
If UserStyle = "考生" Then '如果用户类型是考生
SQL = " select 考生号,密码 from 考生信息表 where 考生号='" & UserID & "'"
ElseIf UserStyle = "教师" Then '如果用户类型是教师
SQL = " select 教师 ID,密码 from 教师信息表 where 教师 ID='" & UserID & "'"
ElseIf UserStyle = "工作人员" Then '如果用户类型是工作人员
SQL = " select 管理人员 ID,密码 from 管理人员信息表 where 管理人员 ID='" & UserID &
"'"
End If
Set rst = SelectSQL(SQL, msg)
If rst.RecordCount > 0 Then '如果存在这个用户
password = Trim(rst.Fields(1)) '取出该用户的密码
rst.Close
Else
MsgBox ("数据库操作错误!")
Exit Sub
End If
'判断密码是否正确
If password = Trim(txtPassword.Text) Then
If UserStyle = "考生" Then '如果用户类型是考生
B 考生考试管理.Show
ElseIf UserStyle = "教师" Then '如果用户类型是教师
A 教师阅卷管理.Show
ElseIf UserStyle = "工作人员" Then '如果用户类型是工作人员
工作人员管理.Show
End If
LoginSucceeded = True
Unload Me
4
剩余15页未读,继续阅读
资源评论
- ynlon2015-01-12谢谢,很好用!
- wxdhlg2013-10-21值得参考,谢谢!
- P454Q2014-01-23不错,不过就是数据库的连接怎么弄没说
- liuboxiaoliu2014-05-14就是没有说清楚怎么用
- licc3122015-08-04谢谢!对我有用。
zhaoyonghui
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功