gpt4 book ai didi

c# - 解决错误 "The ConnectionString property has not been initialized."?

转载 作者:太空狗 更新时间:2023-10-29 22:32:51 25 4
gpt4 key购买 nike

我的代码是查看gridview中的所有数据

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>

在外部类中编码

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;
}
}
}

它在 da.Fill(ds) 行给出错误将数据源与 gridview 绑定(bind)的代码在页面加载时编码如下。

 DataSet ds = new ViewAction().GetAllData();
gvLoginInfo.DataSource = ds;
gvLoginInfo.DataBind();

而数据连接类中的conectionstring代码为

 public static SqlConnection GetConnection()
{

if (con == null)
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
con.Open();
}

return con;
}

一一错误是

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: }

最佳答案

错误仅在 Web.Config 中。请在 connectionString 中的 DataSource 之间加一个空格,如:Data Source。因此您的连接字符串将变为:

 "Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".

关于c# - 解决错误 "The ConnectionString property has not been initialized."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com