2
我一直是“如果它没有破解不修复它”的粉丝 但我的代码,虽然运作,抛出connectionstring属性没有已初始化消息。类似的帖子建议conneciton字符串为空---所以我在我的连接打开命令周围添加了一个if IsNullOrEmpty
。这是抛出异常的线。注意:我的连接字符串是从连接字符串的数据库中获取的。这是.aspx页面文件后面的c#代码。提前感谢您提供有关例外原因的任何建议。ConnectionString属性尚未初始化sqlcommand但代码仍然有效
的代码:
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'的值? – Damith 2013-05-03 04:29:36
你能澄清你的意思吗“抛出异常,但继续工作”?当然,如果抛出异常,你的SqlDataReader代码完全被跳过了,因此它不起作用?这是间歇性故障吗? – 2013-05-03 04:35:48
'sconn.ToString()'永远不会为null或为空。也许'sconn.ConnectionString',取决于'someconnectionstring.Value.ToString()'是什么。 – Corak 2013-05-03 04:39:37