<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; mso-ascii-font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-hansi-font-family:"Times New Roman"; mso-font-kerning:0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:2142259963; mso-list-type:hybrid; mso-list-template-ids:170697532 -654431194 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:宋体; color:windowtext;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
无论是在C#的桌面程序中还是在C#和ASP.NET的网页开发中,数据库连接是必不可少的一步,而且是关键的一步,是很多数据操作的基础,今天连接总是遇到一个问题:
“在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)”
网上的解决办法全部都试了一个遍,但是还是错误依旧,终于请教了我们敬爱的阳哥。阳哥熟练的给我讲解了其中的问题以及关于数据库连接的技巧,受益匪浅,记录下来留作资料吧。
1 .在web.config中有一个节点 <connectionStrings> </connectionStrings>是用来专门设置数据库连接字的,使用该节点可以是编程变得很简单。具体用法如下
connectionStrings 元素(ASP.NET 设置架构)
|
<connectionStrings > <add /> <clear /> <remove /> </connectionStrings> |
属性和元素
下面几部分描述了此元素的属性、子元素和父元素。
属性
只有那些由所有节元素继承的属性。有关更多信息,请参见节元素所继承的常规属性。
子元素
元素 | 说明 |
add | 向连接字符串集合添加名称/值对形式的连接字符串。 |
clear | 移除所有对继承的连接字符串的引用,仅允许那些由当前的 add 元素添加的连接字符串。 |
remove | 从连接字符串集合中移除对继承的连接字符串的引用。 |
父元素
元素 | 说明 |
configuration | 指定公共语言运行库和 .NET Framework 应用程序所使用的每个配置文件中均需要的根元素。 |
system.web | 指定配置文件中 ASP.NET 配置设置的根元素,并包含用于配置 ASP.NET Web 应用程序和控制应用程序行为方式的配置元素。 |
备注
connectionStrings 元素为 ASP.NET 应用程序和 ASP.NET 功能指定数据库连接字符串(名称/值对的形式)的集合。
在以前的 ASP.NET 版本中,连接字符串存储在 appSettings 中。在 ASP.NET 2.0 中,如会话、成员资格、个性化设置和角色管理器等功能均依赖于存储在 connectionStrings 元素中的连接字符串。您还可以使用 connectionStrings 元素来存储您自己的应用程序的连接字符串。
默认配置
除非在子配置文件中使用 clear 元素,否则将继承父配置文件中包含的连接字符串。下面的默认 connectionStrings 元素是在 Machine.config 文件中配置的。
|
|
<connectionStrings> <add name="LocalSqlServer" connectionString="data source=./SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> |
示例
下面的代码示例演示如何配置两个连接字符串。第一个连接字符串可以由 ASP.NET 应用程序使用,而第二个连接字符串可以由稍后配置的 sqlCacheDependency 元素使用。
|
|
<configuration> <!-- Other configuration settings -->
<connectionStrings>
<add name="Sales" providerName="System.Data.SqlClient" connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />
<add name="NorthWind" providerName="System.Data.SqlClient" connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />
</connectionStrings>
<cache> <sqlCacheDependency enabled="true" pollTime="60000"> <databases> <add name="Northwind" /> </databases> </sqlCacheDependency> </cache>
</configuration> |
2. 在使用时应用一下语句
通过ConfigurationManager.ConnectionStrings获得数据库连接字符串用于连接数据库
SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["DBC"].ConnectionString );
3. 如何确定数据库连接字符串,可以说这个问题是这次错误关键点。原来我通过东拼西凑靠书上的描述确定字符串,这样不同的数据库版本MS SQL的连接字符串都不一样,势必导致错误,于是需要VS 2008(VS2005)中供的工具
--》工具—》连接到数据库
通过选取你要连接的服务器名和数据库名,然后选择高级,会弹出如下窗口
可以注意在最下面的 一个box里面正式你所要连接的服务器 和数据库的连接字符串,然后copy 一下,贴到web.config 中的connectionString中的value中