教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 ASP.NET 2.0中连接字符串

ASP.NET 2.0中连接字符串

发布时间:2016-12-05   编辑:jiaochengji.com
教程集为您提供ASP.NET 2.0中连接字符串等资源,欢迎您收藏本站,我们将为您提供最新的ASP.NET 2.0中连接字符串资源

范例程序代码如下: <?xml version="1.0"?><configuration><connectionStrings><add name="Pubs" connectionString="Server=localhost; Integrated Security=True;Database=pubs;Persist Security Info=True" providerName="System.Data.SqlClient" /><add name="Northwind" connectionString="Server=localhost; Integrated Security=True;Database=Northwind;Persist Security Info=True" providerName="System.Data.SqlClient" /></connectionStrings><system.web><pages styleSheetTheme="Default"/></system.web></configuration>

  程序代码说明:在上述范例的程序代码中,我们在Web.Config文件中的<connectionStrings> 配置节点下面设置了两个数据库连接字符串,分别指向pubs和Northwind两个示例数据库。注意,在2.0中引进了数据源控件,例如SqlDataSource 控件,我们可以将SqlDataSource 控件的 ConnectionString 属性被设置为表达式 <%$ ConnectionStrings:Pubs %>,该表达式在运行时由 ASP.NET 分析器解析为连接字符串。还可以为SqlDataSource 的 ProviderName 属性指定一个表达式,例如 <%$ ConnectionStrings:Pubs.ProviderName %>。其具体的用法和新特征将在以后的章节进行详细的介绍。现在有个基础的了解即可。   当然,我们也可以用下面的方式从配置文件直接读取数据库连接字符串。首先我们需要引用using System.Web.Configuration命名空间,该命名空间包含用于设置 ASP.NET 配置的类。 string connectionString =ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

  程序代码说明:在上述范例的程序代码中,我们可以利用ConnectionStrings["Northwind"]读取相应的Northwind字符串。同理以可以利用ConnectionStrings["Pubs"]读取相应的Pubs字符串。

您可能感兴趣的文章:
ASP.NET 2.0中连接字符串
VS2005中连接SQL Server2000 连接字符串
ASP.NET 2.0的新功能
asp.net web.config配置连接字符串的方法
c#连接mysql数据库示例
asp.net 3.5中在web.config文件创建ACCESS连接字符串的方法
python怎么连接字符串
Python数据库API(DB API)
asp.net 在webcofig中连接数据库二种方式
asp.net性能优化方法-字符串操作性能优化

[关闭]
~ ~