C# SqlCommand Connection.Open() issue

独自空忆成欢 提交于 2019-12-05 18:20:54

You need to retrieve the .ConnectionString property :

string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;

using(SqlConnection _con = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con))
{
   // do your stuff here
}

What you're doing right now is just retrieving the whole entry under <connectionStrings> by the name of CRM2Sage.

The problem is, that you are accessing the AppSettings, but you want to access the connection strings:

new SqlConnection(WebConfigurationManager.ConnectionStrings.ConnectionStrings["CRM2Sage"].ConnectionString)

you can use

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;

Or
System.Configuration.ConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;

in asp.net. but not

WebConfigurationManager.AppSettings["CRM2Sage"]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!