C# 解决错误“ConnectionString 属性尚未初始化。”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17509476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Solve the error "The ConnectionString property has not been initialized."?
提问by VJain
My code is view all the data in the gridview
我的代码是查看gridview中的所有数据
Web.config code is
Web.config 代码是
<configuration>
<connectionStrings>
<add name="ConStr" connectionString="DataSource=.;Integrated Security=SSPI;Initial catalog=sshopping"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>
It is coded in external class
它在外部类中编码
namespace DBAction
{
public class ViewAction
{
public DataSet GetAllData()
{
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "Select UserName,Password,RoleName,EmailID,SecurityQuestion,SecurityAnswer,LastLogin from LoginInfo";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Dispose();
DataConnection.CloseConnection();
return ds;
}
}
}
it is giving error in line da.Fill(ds)The code to bind data source with gridview is coded on page load like this.
它在行中给出错误da.Fill(ds)将数据源与 gridview 绑定的代码在页面加载时编码,如下所示。
DataSet ds = new ViewAction().GetAllData();
gvLoginInfo.DataSource = ds;
gvLoginInfo.DataBind();
And conectionstring code in data connection class is
数据连接类中的连接字符串代码是
public static SqlConnection GetConnection()
{
if (con == null)
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
con.Open();
}
return con;
}
And one one error is
一一错误是
Exception Details: System.ArgumentException: Keyword not supported: 'datasource'.
Source Error:
Line 19: {
Line 20: con = new SqlConnection();
Line 21: con.ConnectionString =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Line 22: con.Open();
Line 23: }
采纳答案by R.C
The error is in the Web.Config only. Please put one space between DataSource in connectionString as: Data Source. Thus your connection String will become:
错误仅在 Web.Config 中。请在 connectionString 中的 DataSource 之间放置一个空格作为:Data Source。因此您的连接字符串将变为:
"Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".
回答by asafrob
From the examples i see online, in your connection string replace "DataSource" with "Data Source" (with a space between the two words). http://msdn.microsoft.com/en-us/library/ms156450.aspx
从我在网上看到的示例中,在您的连接字符串中,将“DataSource”替换为“Data Source”(两个词之间有一个空格)。 http://msdn.microsoft.com/en-us/library/ms156450.aspx
回答by JunkCK
I was getting same error, even after made all changes you mentioned above. And after that, I change my code in that way:
即使进行了您上面提到的所有更改,我也遇到了同样的错误。在那之后,我以这种方式更改了我的代码:
My Web.Config:
我的 Web.Config:
<connectionStrings>
<add name="conStr" connectionString="Data Source=SomeDS;Initial Catalog=SomeCatalog;User Id=myId;Password=myPass" />
</connectionStrings>
My old Code:
我的旧代码:
string ConnectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (var sqlConnection = new SqlConnection(ConnectionString ))
{
var result= sqlConnection.Query<My_Class>("MY_PROC_NAME", new { count }, commandType: CommandType.StoredProcedure);
return result;
}
My newCode:
我的新代码:
var dt = new DataTable();
using (var cnn = new SqlConnection(ConnectionString ))
using (var cmd = new SqlCommand("MY_PROC_NAME", cnn))
{
cmd.CommandType = CommandType.StoredProcedure;
cnn.Open();
var sqlReader = cmd.ExecuteReader();
if (sqlReader.HasRows)
dt.Load(sqlReader);
cnn.Close();
}
result = dt.DataTableToList<My_Class>();
DataTableToList is a converter method, if you need i can share it, too.
DataTableToList 是一个转换器方法,如果你需要我也可以分享它。

