我正在尝试将连接字符串写入 Web.config,如下所示:
<connectionStrings>
<add name="Dbconnection" connectionString="Server=localhost;
Database=OnlineShopping ; Integrated Security=True"/>
</connectionStrings >
并像这样读取它:
string strcon =
ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);
运行程序时,由于空引用而出现错误。但是当我使用这段代码时:
SqlConnection DbConnection = new SqlConnection();
DbConnection.ConnectionString =
"Server=localhost; Database=OnlineShopping ; Integrated Security=True";
我没有收到任何错误,程序正常运行!问题是什么?