如何在数据源向导中使用APP.Config中定义的connectionString? 我可以在Web应用程序中轻松完成,但我无法在非Web应用程序中执行此操作。
答案 0 :(得分:5)
您需要使用bellow语法从app.config
中检索连接字符串string conStr = ConfigurationManager.ConnectionStrings["NorthwindConnection"].ConnectionString;
然后使用下面的语法。首先,您需要使用System.Configuration添加; 名称空间
Connection = new SqlConnection(conStr);
答案 1 :(得分:1)
尝试使用ConfigurationManager类。 这应该做:
string myConnString = ConfigurationManager.ConnectionStrings["myConnStringName"].ConnectionString;