27.sqlserver2005链接字符串--2013-06-08

习惯用OLEDB+链接字符串去连数据库了,以前连sqlserver2000一直正常,由于换了个Y400的本本,装了win7,而后顺便sqlserver也搞了个64位的2005的。问题出现了,以前的数据库都连不上了。开始找了很久没找到缘由,后来发现2005中uid跟pwd替换了以前的user跟password。顺便整理下链接字符串以下。 html

转帖的一些文章整理下,原文连接在文中。 sql

 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;
 

本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/jyh_jack/archive/2008/04/07/2257512.aspx 
 
Sql Server2005链接字符串总结

SqlConnection (.NET) 数据库

       标准安全链接 express

如下是语法格式:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

使用"服务器名/实例名"的格式做为Data Source来链接到一个Sql Server服务器上的一个实例。 windows

你用过sql server 2005 Express么?不要忘记服务器的语法“Servername/SQLEXPRESS”(你要把Servername替换为装有sql server 2005 Express的计算机的名字) 安全

     标准链接的另外一种语法 服务器

这个链接字符串和前一个的做用是同样的,只因此放到这里是为了说明有些链接参数带有相同的效果。 网络


如下是语法格式:
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;

    信任的链接 app


如下是语法格式:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

       信任链接的另外一种语法 asp.net

这个链接字符串和前一个的做用是同样的,只因此放到这里是为了说明有些链接参数带有相同的效果。


如下是语法格式:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    链接到数据库实例

这个指名Sql Server 实例的语法中的server参数的值对全部链接Sql Server的字符串相同。

如下是语法格式:
Server=myServerName/theInstanceName;Database=myDataBase;Trusted_Connection=True;

     信任链接来至CE设备

一般CE设备是没有验证和登录到域的。用SSPI或信任验证的链接要使用下面的链接字符串。

如下是语法格式:
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;

DBMSSOCN=TCP/IP.这是指明使用IP地址而不是命名管道来链接。Data Source的最后是端口。1433是数据库的默认端口。

   容许多个结果集(Enable Mars (multiple active result sets))


如下是语法格式:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;MultipleActiveResultSets=true;

此功能只支持Ado.net 2.0.

   在一个到本地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 实例上的用户实例。

用户实例的链接建立了一个新的SQL Server 实例。此链接只能是在本地SQL Server 2005实例上而且是经过命名管的windows验证链接才有效。目的就是为了给用户建立一个彻底权限的Sql Server 实例和有限的计算机管理员权限。


如下是语法格式:
Data Source=./SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|/mydb.mdf;User Instance=true;

要使用User Instance 功能,你要在sql server服务器上启用他。你能够经过执行“sp_configure 'user instances enabled', '1'”来启用,执行“sp_configure 'user instances enabled', '0'”来禁用。

    数据库镜像

若是你用Ado.Net或着SQL Native Client 链接到一个镜像的数据库,当一个数据库镜像出错时,你的应用程序应该提供自动切换链接的功能。那么,你应该在链接字符串中指明原始数据库名和镜像数据库名。


如下是语法格式:
Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;

固然还有不少链接数据库镜像的链接字符串的写法。这只是一个例子指明 failover功能。你还能够指明其余的链接字符串属性参数。

    异步处理

SQL Server 2005容许经过Ado.net对象进行异步请求任务。


如下是语法格式:
Server=myServerAddress;Database=myDataBase;Integrated Security=True;Asynchronous Processing=True;

 

 

SQL Server 2005 specials

 

   Context链接

Connecting to "self" from within your CLR stored prodedure/function. The context connection lets you execute Transact-SQL statements in the same context (connection) that your code was invoked in the first place.


如下是语法格式:
C#
using(SqlConnection connection = new SqlConnection("context connection=true"))
{
      connection.Open();
      // Use the connection
}

======================================================================

转载声明:本文转自http://hi.baidu.com/shangfei2009/blog/item/c1bbae3c901de7e73d6d97bc.html

在此谨对原创做者和转载分享网友,致以深深的谢意,也愿更多的网友可以分享和学习经典资料,谢谢!

===================================================

对于SQLServerExpress数据库链接配置,之前看到过几回,都是匆匆扫一眼,今天上午在一开源软件中又看到了它,感受有必要对它有一个清楚的认识,示例以下:
<add name="LocalSqlServer" connectionString="Data Source=./SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|TimeTracker.mdf;User Instance=true" />
SqlConnectionStringBuilder实例化时,要用到 connectionString,如:SqlConnectionStringBuild builder = new SqlConnectionStringBuild(connectionString)。

   1、Data Source
SqlConnectionStringBuilder 的DataSource属性,对应connectionString中的Data Source,“Data Source”能够由下列字符串代替:“server”,“address”,“addr”和“network address”。
Data Source=./SQLExpress也能够写成这样Data Source=(local)/SQLExpress。

   2、Integrated Security
SqlConnectionStringBuilder 的IntegratedSecurity属性,对应connectionString中的Integrated Security,“Integrated Security”能够写成“trusted_connection”,为true时,使用当前的 Windows 账户凭据进行身份验证,为false时,须要在链接中指定用户 ID 和密码。

   3、AttachDBFilename
SqlConnectionStringBuilder 的AttachDBFilename属性,对应connectionString中的 AttachDBFilename,“AttachDBFilename”能够写成“extended properties”,“initial file name”。AttachDbFileName属性指定链接打开的时候动态附加到服务器上的数据库文件的位置。这个属性能够接受数据库的完整路径和相对路 径(例如使用|DataDirectory|语法),在运行时这个路径会被应用程序的App_Data目录所代替。
有一篇文章,专门解释DataDirectory是什么。

   4、User Instance
SqlConnectionStringBuilder 的UserInstance属性,对应connectionString中的User Instance ,该值指示是否将链接从默认的 SQL Server Express 实例重定向到在调用方账户之下运行而且在运行时启动的实例。UserInstance=true,在这种状况下,SQLServerExpress为了把 数据库附加到新的实例,创建一个新的进程,在打开链接的用户身份下运行。在ASP.NET应用程序中,这个用户是本地的ASPNET账号或默认的 NetworkService,这依赖于操做系统。为了安全地附加非系统管理员账号(例如ASP.NET账号)提供的数据库文件,创建一个独立的 SQLServer用户实例是必要的。


附 录:

DataDirectory是什么?

asp.net  2.0有一个特殊目录app_data,一般Sql  server  2005  express数据文件就放在这个目录,相应的数据库链接串就是:
connectionString="……  data  source=./SQLEXPRESS;Integrated  Security=SSPI;AttachDBFilename=|DataDirectory|data.mdf;User  Instance=true"
这里有一个DataDirectory的宏,它表示什么意义呢?

DataDirectory是表示数据库路径的替换字符串。因为无需对完整路径进行硬编码,DataDirectory  简化了项目的共享和应用程序的部署。例如,无需使用如下链接字符串: 
"Data  Source=  c:/program  files/MyApp/app_data/Mydb.mdf" 
经过使用|DataDirectory|(包含在以下所示的竖线中),便可具备如下链接字符串: 
"Data  Source  =  |DataDirectory|/Mydb.mdf" 。

不只仅是Sql server 2005 express中使用,也能够在其它的文件数据库中使用,例如Sqllite数据库文件的链接字符串:

<add  name="DefaultDB" 
connectionString="DriverClass=NHibernate.Driver.SQLite20Driver;Dialect=NHibernate.Dialect.SQLiteDialect;Data  Source=|DataDirectory|/data.db3" />

 

2.链接数据库服务器

<!--链接数据库服务器-->
  <add key="CRMonnection" value="Data Source=Sufei;Initial Catalog=CRM;Integrated Security=false;uid=1233;Password=123456"></add>

通常是这样写的,意思 我就不在多说了你们一看就知道了。

3.怎么样取呢来看个例子吧

先看下面的代码

 


<appSettings>
    
<!--链接数据库服务器-->
        
<add key="CRMonnection" value="Data Source=Sufei;Initial Catalog=CRM;Integrated Security=false;uid=nouter;Password=123456"></add>
        
<add key="ForumName" value="漯河移动"/>
    
<!--链接数据库文件-->
    
<add key ="CRMConnectionString" value ="Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|CRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
    
</appSettings>

取的方法在这里

 System.Configuration.ConfigurationSettings.AppSettings["CRMConnectionString"];
相关文章
相关标签/搜索