在ASP.NET 2.0中,使用了一种在运行时解析为连接字符串值的新的声明性表达式语法,按名称引用数据库连接字符串。连接字符串本身存储在 Web.config 文件中的<connectionStrings>
范例程序代码如下:
<?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["Northwind"]读取相应的Northwind字符串。同理以可以利用ConnectionStrings["Pubs"]读取相应的Pubs字符串。