【问题标题】:The ConnectionString property has not been initialized sqlcommand yet code still worksConnectionString 属性尚未初始化 sqlcommand 但代码仍然有效
【发布时间】:2013-05-03 13:55:33
【问题描述】:

我一直是“如果它没有坏就不要修复它”的粉丝 但是我的代码虽然可以正常运行,但会抛出 connectionstring 属性尚未初始化消息。类似的帖子表明连接字符串为空---所以我在我的连接打开命令周围添加了一个if IsNullOrEmpty。这是引发异常的行。注意:我的连接字符串是从连接字符串数据库中检索的。这是 .aspx 页面的 c# 代码隐藏文件。预先感谢您对异常原因提出任何建议。

代码:

using (SqlConnection sconn = new SqlConnection(someconnectionstring.Value.ToString()))
        {
            using (SqlCommand scmd = new SqlCommand("mydb.[dbo].[myStoredProc]", sconn))
            {
                scmd.CommandType = CommandType.StoredProcedure;
                scmd.Parameters.Add("@valueX", SqlDbType.VarChar).Value = 2; 
                scmd.Parameters.Add("@returnValue", SqlDbType.Int);
                scmd.Parameters["@returnValue"].Direction = ParameterDirection.Output;
        //testing for null as suggested...
        if (!string.IsNullOrEmpty(sconn.ToString()) || sconn.ToString() != "")   //the ||  != "" may be double work.. not sure
                {
                    sconn.Open();  //code throw exception here but continues to work.
                    SqlDataReader adar = scmd.ExecuteReader();
                    if (adar.HasRows)
                    {

                        while (adar.Read())
                        {
                            hiddenfieldX.Value = adar["valueX"].ToString();
            ...
                        }
                        sconn.Close();
                    }
                }
            }
        }
    }
    catch (SqlException er)
    {
    //The ConnectionString property has not been initialized thrown here
    } 

【问题讨论】:

  • someconnectionstring.Value的值是多少?
  • 你能解释一下“抛出异常但继续工作”的意思吗?当然,如果抛出异常,您的 SqlDataReader 代码将被完全跳过,因此它不起作用?是间歇性故障吗?
  • sconn.ToString() 永远不会是 null 或为空。也许sconn.ConnectionString,取决于someconnectionstring.Value.ToString() 是什么。

标签: c# code-behind sqlconnection


【解决方案1】:

正如队友所说,连接字符串可能为空或格式不正确。

1-检查您的连接字符串是否正确(您可以发布它,我们可以检查)

2- 如果您的代码看起来像这样,您的代码会更好:

string someconnectionstring = "yourConnectionString";

if (!string.IsNullOrEmpty(someconnectionstring))
{
    using (SqlConnection sconn = new SqlConnection(someconnectionstring))
    {
        using (SqlCommand scmd = new SqlCommand("mydb.[dbo].[myStoredProc]", sconn))
        {
            scmd.CommandType = CommandType.StoredProcedure;
            scmd.Parameters.Add("@valueX", SqlDbType.VarChar).Value = 2;
            scmd.Parameters.Add("@returnValue", SqlDbType.Int);
            scmd.Parameters["@returnValue"].Direction = ParameterDirection.Output;

            sconn.Open();  //code throw exception here but continues to work.
            SqlDataReader adar = scmd.ExecuteReader();
            if (adar.HasRows)
            {

                while (adar.Read())
                {
                    //...
                }
            }                
            sconn.Close();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多