Home > Database > Mysql Tutorial > body text

SQLServer 2005数据库连接字符串 连接sql2005必备资料

WBOY
Release: 2016-06-07 18:05:12
Original
826 people have browsed it

现在好多朋友开始用sqlserver 2005了,因为跟sql2000有区别,所以这里整理了下,需要的朋友可以参考下。

连接字符串中常用的声明有:

服务器声明 Data Source、Server和Addr等。

数据库声明 Initial Catalog和DataBase等。

集成Windows账号的安全性声明 Integrated Security和Trusted_Connection等。

使用数据库账号的安全性声明 User ID和Password等。

对于访问数据库的账号来说,通常我们在一些参考资料上看到ADO.NET的字符串连接往往有如下写法:
代码如下:

string ConnStr = "server = localhost;
user id = sa; password = xxx; database = northwind";

对于集成Windows安全性的账号来说,其连接字符串写法一般如下:

代码如下:
string ConnStr = "server = localhost;
integrated security = sspi; database = northwind";
或string ConnStr = "server = localhost;
trusted_connection = yes; database = northwind";

使用Windows集成的安全性验证在访问数据库时具有很多优势:安全性更高、访问速度更快、减少重新设计安全架构的工作、可以硬编码连接字符串等,还是很值得使用的。





SQL Native Client ODBC Driver

标准安全连接
代码如下:Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

您是否在使用SQL Server 2005 Express? 请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。

受信的连接
代码如下:Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;

"Integrated Security=SSPI" 与 "Trusted_Connection=yes" 是相同的。

连接到一个SQL Server实例
指定服务器实例的表达式和其他SQL Server的连接字符串相同。
Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=yes;


指定用户名和密码
oConn.Properties("Prompt") = adPromptAlways

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;


使用MARS (multiple active result sets)

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;MARS_Connection=yes;

"MultipleActiveResultSets=true"与MARS_Connection=yes"是相同的。
使用ADO.NET 2.0作为MARS的模块。 MARS不支持ADO.NET 1.0和ADO.NET 1.1。

验证网络数据

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;


使用附加本地数据库文件的方式连接到本地SQL Server Express实例

Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

使用附加本地数据文件夹中的数据库文件的方式连接到本地SQL Server Express实例

Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

数据库镜像
Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;



SQL Native Client OLE DB Provider

标准连接

Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

您是否在使用SQL Server 2005 Express? 请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。

受信的连接

Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;

"Integrated Security=SSPI"与"Trusted_Connection=yes"相同

连接到SQL Server实例
指定服务器实例的表达式和其他SQL Server的连接字符串相同。
Provider=SQLNCLI;Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=yes;


使用帐号和密码
oConn.Properties("Prompt") = adPromptAlways

oConn.Open "Provider=SQLNCLI;Server=myServerAddress;DataBase=myDataBase;


使用MARS (multiple active result sets)

Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;MarsConn=yes;

"MultipleActiveResultSets=true"和"MARS_Connection=yes"是相同的。
使用ADO.NET 2.0作为MARS的模块。 MARS不支持ADO.NET 1.0和ADO.NET 1.1。

验证网络数据

Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;


使用附加本地数据库文件的方式连接到本地SQL Server Express实例

Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

使用附加本地数据文件夹中的数据库文件的方式连接到本地SQL Server Express实例

Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

数据库镜像
Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;



SqlConnection (.NET)

标准连接

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

使用serverName\instanceName作为数据源可以指定SQL Server实例。
您是否在使用SQL Server 2005 Express? 请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。

Standard Security alternative syntax
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;


受信任的连接

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;


Trusted Connection alternative syntax
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;


连接到一个SQL Server的实例
指定服务器实例的表达式和其他SQL Server的连接字符串相同。
Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;


来自WinCE设备的安全连接
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;

仅能用于CE设备。

带有IP地址的连接

Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

使用MARS (multiple active result sets)

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;MultipleActiveResultSets=true;

使用ADO.NET 2.0作为MARS的模块。 MARS不支持ADO.NET 1.0和ADO.NET 1.1。

使用附加本地数据库文件的方式连接到本地SQL Server Express实例

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

使用附加本地数据文件夹中的数据库文件的方式连接到本地SQL Server Express实例

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。

使用在SQL Server Express实例上的用户实例
Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\mydb.mdf;User Instance=true;

数据库镜像
Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;

Asynchronous processing
Server=myServerAddress;Database=myDataBase;Integrated Security=True;Asynchronous Processing=True;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!