动态连接数据库

dephi连接方法:
procedure TloginForm.FormCreate(Sender: TObject);
var
currentPath,str,path:String;
begin

path:=ExtractFilePath(Application.ExeName );
currentPath:=concat(path,'HomeInfoSystem.mdb');
str:='Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;';
str:=concat(str,'Data Source=',currentpath);
ADOConnection.ConnectionString:=str;
ADOTable.Connection:=ADOConnection;
ADOTable.TableName:='成员列表';
ADOTable.Active:=True; ;
ADOTable.Open ;

end;


vb连接方法1(ADO):
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Public Sub OpenDBConn(ByVal strPath As String)
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source = " & strPath & "/your_db.mdb"
conn.Open
End Sub

Private Sub Form_Load()
OpenDBConn (App.Path)
Set rs = conn.Execute("select * from userInfo")
End Sub

(2)
'用此方法操作数据库需引用DOA对象
Dim dbs As Database
Dim rst As Recordset
Dim strName As String
Dim strPwd As String

Private Sub Form_Load()
Set dbs = OpenDatabase(App.Path & "/db.mdb")
Set rst = dbs.OpenRecordset("user")
addNewX
End Sub
Private Sub addNewX()
strName = InputBox("请输入姓名;", "对话:", "")
strPwd = InputBox("请输入密码;", "对话:", "")
If strName <> "" And strPwd <> "" Then
addCord rst, strName, strPwd
Else
MsgBox "姓名或密码不能为空"
End If
End Sub
Private Sub addCord(rs As Recordset, name As String, pwd As String)
With rs
.AddNew
!name = name
!pwd = pwd
.Update
End With

End Sub

你可能感兴趣的:(数据库)