Public Sub Pr_EPD_DBConn()
On Error GoTo Err
Set conn = New adodb.Connection
Set cmd = New adodb.Command
Set Rs = New adodb.Recordset
conn.ConnectionString = "Provider=SQLOLEDB.1;Password=zxg;Persist Security Info=True;User ID=zxg;Initial Catalog=yourdatabasename;Data Source=dataservername"
conn.CursorLocation = adUseClient
conn.CommandTimeout = 30
' conn.Mode = adModeReadWrite
conn.Open
Exit Sub
Err:
msg = "数据库联接失败,请与系统管理员联系!"
MsgBox msg, vbCritical, title
End Sub
Private Function BlnParent_Code_Query() As Boolean
Call Pr_EPD_DBConn ' open database
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = "select * from tablename"
.Execute
End With
With Rs
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockOptimistic
.Open cmd
If Not .EOF Then
Set DataGrid2.DataSource = Rs
End If
End With
End Function
private sub command1_click()
BlnParent_Code_Query