delphi连接sql server的字符串2011-10-11 16:07

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=""' ;

posted @ 2014-12-27 23:22  老榕树  阅读(718)  评论(0编辑  收藏  举报