DelPhi连接数据库方式

一、SQL Server 2000 的连接数据库
1.无密码连接
SQLL:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=test;Data Source=192.168.0.23';                                      //catalog 指数据库名称 datasource 指 计算机名称,也
Data.ADOCnn.ConnectionString:=SQLL;        //可以是ip地址
Data.ADOCnn.Connected:=false;
Data.ADOCnn.LoginPrompt:=false;
try
   Data.ADOCnn.Connected:=true;
except                                          //连接失败处理
    begin
     MessageBox(0,'连接数据库失败!','提示',64);
     Application.Terminate;
    end;
end;
2.有密码连接 (密码为123)
   SQLL:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PassWord=123;Initial Catalog=test;Data Source=192.168.0.23'; 

二、Access 数据库的连接
1.无密码,决对路径连接
AdoConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= '
                                        + OpenDialog1.FileName; //绝对路径,这里是用打开文件的路径
      AdoConnection1.LoginPrompt := False;
      AdoConnection1.Connected := True;

2. 有密码,相对路径连接
pwd:='123';
SQLL:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
         extractfilepath(application.ExeName)+'db1.mdb'+
         ';Persist Security Info=False;Jet OLEDB:Database Password="'+pwd+'"';
Form1.ADOConn.Connected:=false;
Form1.ADOConn.LoginPrompt:=false;
Form1.ADOConn.ConnectionString:=SQLL;
try
    Form1.ADOConn.Connected:=true;                 //连接数据库
except
    begin
     MessageBox(0,'连接数据库失败!','提示',64);
     Application.Terminate;
end;
end;
三、选择数据库连接(access)
procedure TFormMainWindow.FormCreate(Sender: TObject);
begin
FTableList := TStringList.Create;
end;

//按钮点击事件

OpenDialog1.Filter := '数据库文件(*.*)';
if OpenDialog1.Execute then
begin
    try
      if AdoConnection1.Connected then AdoConnection1.Connected := False;
      AdoConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= '
                                        + OpenDialog1.FileName;
      AdoConnection1.LoginPrompt := False;
      AdoConnection1.Connected := True;
      AdoConnection1.GetTableNames(FTableList);           //FTableList : Tstings
      CboTables.Items := FTableList;                                 //CboTables:combobox
posted @ 2013-07-19 13:41  盛开的雨季  阅读(681)  评论(0编辑  收藏  举报