Office中国论坛/Access中国论坛
标题:
Access-ODBC-SQL数据连接安全性如此提高
[打印本页]
作者:
tsilon
时间:
2010-11-20 23:39
标题:
Access-ODBC-SQL数据连接安全性如此提高
本帖最后由 红尘如烟 于 2010-11-21 11:36 编辑
先用带密码的connection语句刷,后用不带密码的,如下
Option Compare Database
Option Explicit
Public Sub ReLinkTbl()
On Error GoTo Err_Dot:
DoCmd.Echo False '关闭屏幕更新
DoCmd.Hourglass True '将光标设为沙漏形以表示系统正在进行后台处理
DoCmd.SetWarnings False '关闭系统警告提示消息
Dim mydb As DAO.Database
Dim MyTable As DAO.TableDef
Dim strTblName As String
Dim MySet As DAO.Recordset
Dim i As Integer, j As Integer
Dim connectstr As String
Dim intFlag As Boolean
Dim strAttributes As String
RefreshTblSysTables '当添加了新的链接表的时候则需要此行
connectstr = "ODBC;DSN=DBACD;"
'connectstr = connectstr & "SERVER=XXX;"
connectstr = connectstr & "APP=Microsoft Office 2003;" '"WSID= " & XXX & " ;"
connectstr = connectstr & "DATABASE=XXX;"
connectstr = connectstr & "UID=XXX;"
connectstr = connectstr & "PWD=XXX;"
connectstr = connectstr & "QuotedId=No;AnsiNPW=Yes;"
connectstr = connectstr & "LANGUAGE=us_english;AutoTranslate=No" & Chr(0)
'DoEvents---执行大批量操作时添加此行以移交系统控制权,使系统可以处理其它事件并防止程序假死.
Set mydb = DBEngine.Workspaces(0).Databases(0)
Set MySet = CurrentDb.OpenRecordset("TblSysTables", dbOpenTable, dbSeeChanges, dbOptimistic)
If MySet.RecordCount > 0 Then
MySet.MoveFirst
For j = 1 To MySet.RecordCount
intFlag = True
strTblName = MySet.Fields("TblName")
Set MyTable = mydb.TableDefs(strTblName)
MyTable.Connect = connectstr
MyTable.RefreshLink
If intFlag Then
MySet.Edit
MySet.Fields("ConnectFlag") = 1
MySet.Fields("Flag") = "OK"
'MySet.Fields("TblPropertiy") = CurrentDb.TableDefs(strTblName).Connect
MySet.Update
End If
MySet.MoveNext
Err.Clear
Next
Else
Exit Sub
End If
' 使用没有密码的连接字串更新链接表的Connect属性,避免泄露密码(否则密码会被保存在MSysObjects表中)
connectstr = "ODBC;DSN=XXX;"
'connectstr = connectstr & "SERVER=XXX;"
connectstr = connectstr & "APP=Microsoft Office 2003;" '"WSID= " & XXX & " ;"
connectstr = connectstr & "DATABASE=XXX;"
connectstr = connectstr & "QuotedId=No;AnsiNPW=Yes;"
connectstr = connectstr & "LANGUAGE=us_english;AutoTranslate=No" & Chr(0)
If MySet.RecordCount > 0 Then
MySet.MoveFirst
For j = 1 To MySet.RecordCount
intFlag = True
strTblName = MySet.Fields("TblName")
Set MyTable = mydb.TableDefs(strTblName)
MyTable.Connect = connectstr
MyTable.RefreshLink
If intFlag Then
MySet.Edit
MySet.Fields("ConnectFlag") = 1
MySet.Fields("Flag") = "OK"
MySet.Update
End If
MySet.MoveNext
Err.Clear
Next
End If
GoTo Exit_Port
Err_Dot:
If Err <> 0 Then
MsgBox "刷新链接表--->错误源: " & "-->" & Err.Number & "---" & Err.Description, vbOKOnly, "Tsilon sys!"
GoTo Exit_Port
End If
Exit_Port:
If Not (MySet Is Nothing) Then
MySet.Close
End If
Set MySet = Nothing
DoCmd.Echo True '恢复屏幕更新
DoCmd.Hourglass False '恢复光标形状
DoCmd.SetWarnings True '恢复系统警告提示消息
Exit Sub
End Sub
Public Sub RefreshTblSysTables()
On Error Resume Next
Dim MyTbl As DAO.TableDefs, i As Integer
Dim rsSet As DAO.Recordset
DoCmd.RunSQL "Delete * From TblSysTables"
Set MyTbl = CurrentDb.TableDefs
Set rsSet = CurrentDb.OpenRecordset("TblSysTables")
For i = 0 To MyTbl.Count - 1
If MyTbl(i).Attributes = DB_ATTACHEDODBC Or MyTbl(i).Attributes = DB_ATTACHEDODBC + DB_ATTACHSAVEPWD Then
rsSet.AddNew
rsSet.Fields("ConnectFlag") = 0
rsSet.Fields("TblName") = MyTbl(i).Name
rsSet.Update
End If
Next
rsSet.Close
Set rsSet = Nothing
End Sub
复制代码
作者:
t小宝
时间:
2010-11-21 10:29
能否有文字简要说明一下
作者:
红尘如烟
时间:
2010-11-21 11:33
帮你把注释改成中文的了,不过弄的那个日志表感觉没什么必要
作者:
todaynew
时间:
2010-11-21 19:55
学习一下
作者:
xuwenning
时间:
2010-11-22 08:50
谢谢分享
学习了
作者:
termisss
时间:
2010-11-22 09:48
上班之余,来逛逛论坛.
作者:
yanwei82123300
时间:
2010-11-22 10:04
谢谢分享
学习了
作者:
lirong
时间:
2010-12-9 22:33
为什么我再次连时提示输入密码
作者:
67613188
时间:
2010-12-11 14:46
有个实例的话一路了然了,谢谢
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3