执行SqlCommand.ExecuteScalar()时,Visual Studio停止调试

时间:2016-06-20 11:12:56

标签: c# visual-studio ado.net

我在我的应用程序中有这段代码

SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;

sqlConnection1.Open();

cmd.CommandText = "SELECT S2 FROM CDUSR WHERE N100 = " + N100.ToString()  ;

var HashStr = cmd.ExecuteScalar();
sqlConnection1.Close();

当我到达

行时
var HashStr = cmd.ExecuteScalar();

Visual Studio 2015停止调试我的应用程序而没有任何错误。

connectionString包含有效的连接字符串(我已测试过)

此查询SELECT S2 FROM CDUSR WHERE N100 = 6369在SSMS中成功执行。

我想知道问题所在。 S2包含以下字符串:

a4sd4545ag5645sdfa55sdf45fv5er5cfvg5s45

2 个答案:

答案 0 :(得分:2)

您是否正在为SqlCommand使用正确的构造函数?在我看来,你至少缺席了:

cmd.Connection = sqlConnection1

创建命令时,您可以在构造函数中执行此操作:

SqlCommand cmd = new SqlCommand("SELECT S2 FROM CDUSR WHERE N100 = " + N100.ToString()), sqlConnection1)

使用块也会很好:

using(SqlCommand cmd = new SqlCommand(blah blah)) { cmd.ExecuteScalar; }

答案 1 :(得分:1)

我想Visual Studio会停止调试,因为你的代码会引发异常(你的SqlCommand与你使用的SqlConnection没有关联)...也许你的“异常设置”中禁用了“公共语言运行时异常”,所以你不要实际上看到抛出的异常......