解决“ConnectionString属性未初始化”错误?

5

我的代码可以查看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的代码是在页面加载时编写的,如下所示。

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

在数据连接类中,连接字符串代码如下:
 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:             }

在你的代码中有一个空的异常块,请先检查它,然后再费力地询问错误是什么。 - Valamas
你能否在网站配置文件中发布你的连接字符串? - asafrob
是的,先生,我已经发布了 @asafrob。 - VJain
3个回答

4
错误只出现在Web.Config文件中。请在connectionString中的DataSource之间加一个空格,写为:Data Source。这样你的连接字符串就会变成:
 "Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".

4

1
我一直在遇到同样的错误,即使我已经按照你上面提到的所有更改进行了修改。之后,我按照以下方式更改了我的代码:
我的Web.Config:
<connectionStrings>
    <add name="conStr" connectionString="Data Source=SomeDS;Initial Catalog=SomeCatalog;User Id=myId;Password=myPass" />
</connectionStrings>

我的旧代码:

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

我的新代码:

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是一个转换器方法,如果需要的话我可以分享给你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接