delphi串連sql server的字串2011-10-11 16:07

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.