標籤:
delphi串連sql server的字串2011-10-11 16:07
一、delphi串連sql server
放一個串連組件 ADOConnection, 其它組件TADODataSet,TADOQuery等的connection指向ADOConnection就可以了.
你可以雙擊ADOConnection,使用它的嚮導。也可以使用下面的代碼
function OpenADOConn:boolean;
begin
result:=false;
try
with ADOConnection do
begin
Connected:= false;
Provider:= ‘SQLOLEDB.1‘;
Properties[‘Data Source‘].Value:= HostName; //伺服器名
Properties[‘Initial Catalog‘].Value:= DatabaseName; //表名
Properties[‘User ID‘].Value:= UserID; //使用者名稱
Properties[‘password‘].Value:= UserPWD; 密碼
LoginPrompt:= false;
try
Connected:= true;
except
begin
Application.MessageBox(‘無法連結遠端資料庫!‘
,‘注意‘, MB_OK);
exit;
end;
end;
end;
finally
end;
result:=true;
end;
二、Delphi 串連 SQL Server 2005
唯一的關鍵就是連接字串,別的都一樣
SQL Server 2005 標準連接字串:
NT 帳戶登入:
Provider=SQLNCLI.1;
Persist Security Info=True;
User ID={user ID};
Password={password};
Initial Catalog={database name};
Data Source={instance name};
SQL 帳戶登入:
Provider=SQLNCLI.1;
Integrated Security=SSPI;
Persist Security Info=False;
Initial Catalog={database name};
Data Source={instance name};
其中 user ID和 password 就不用說了,分別是使用者名稱和密碼
database name 是資料庫的名稱
instance name 是 SQL Server 執行個體的名稱,注意,這個執行個體必須指明使用者
例如我的電腦名稱是 RARNU,IP是 192.168.0.100
那麼instance name可以填入 RARNU\SQLSERVER2005 或 192.168.0.100\SQLSERVER2005
後面的 SQLSERVER2005 是安裝時指定的執行個體名稱。
接下來的事情就很簡單了,在Delphi中寫如下代碼:
ADOConnection1.ConnectionString :=
‘Provider=SQLNCLI.1;‘+
‘Integrated Security=SSPI;‘+
‘Persist Security Info=False;‘+
‘Initial Catalog=demo;‘+
‘Data Source=.\SQLEXPRESS;‘;
ADOConnection1.Open;
三,串連2008資料庫的字串;
/LinkConnectionStr := ‘Provider=SQLNCLI10.1;Server=‘+cbDBServer.Text+‘;Database=‘+cbDBname.Text+‘;User ID=‘+edtUser.Text+‘;Password=‘+medtPwd.Text+‘;‘;
//LinkConnectionStr :=‘Provider=SQLNCLI10.1;Integrated Security="";Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=伺服器名\mssql2008;Initial File Name="";Server SPN=""‘ ;
delphi串連sql server的字串2011-10-11 16:07