简体   繁体   English

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

[英]Visual Studio stops debugging when SqlCommand.ExecuteScalar() is executed

I have this code in my application 我的应用程序中有此代码

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();

When I reach at the line 当我到达终点线

var HashStr = cmd.ExecuteScalar();

Visual Studio 2015 stops debugging my application without any error. Visual Studio 2015停止调试我的应用程序,没有任何错误。

connectionString contains a valid connection string (I have tested) connectionString包含有效的连接字符串(我已经测试过)

This query SELECT S2 FROM CDUSR WHERE N100 = 6369 executes successfully in SSMS. 此查询SELECT S2 FROM CDUSR WHERE N100 = 6369在SSMS中成功执行。

I want to know what the problem is. 我想知道问题是什么。 S2 contains the following string: S2包含以下字符串:

a4sd4545ag5645sdfa55sdf45fv5er5cfvg5s45

Are you using the correct constructor for the SqlCommand? 您是否使用了正确的SqlCommand构造函数? It seems to me that you are atleast missing: 在我看来,您至少是失踪了:

cmd.Connection = sqlConnection1

You might as well do that in the constructor when you create the command: 创建命令时,最好在构造函数中执行以下操作:

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

a using block would be nice as well: using块也将很好:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM