Visual Studio 2012例外 [英] Visual Studio 2012 Exceptions

查看:79
本文介绍了Visual Studio 2012例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道visual studio 2012会发生什么?当我在Visual Studio 2010中运行以下代码时,它按预期正常工作。但是当我在Visual Studio 2012中运行相同的代码时,它既没有抛出异常,也没有通知我异常。有人可以告诉我这件事吗?



<前lang =c#> 字符串 connectionString = < span class =code-string> Data Source = xyz; Initial Catalog = abc; User ID = sa; Password = 123; Integrated Security = true;
SqlConnection sqlConnection = new SqlConnection(connectionString);
尝试
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand( SELECT COUNT(*)FROM tblProduct);
sqlCommand.Connection = sqlConnection;

int RecordCount = Convert.ToInt32(sqlCommand.ExecuteScalar());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}

解决方案

也许,这有帮助:在Debug中设置VisualStudio - >例外 - >抛出.Net异常停止。

然后再次调试代码:调试器将停在抛出位置。

干杯

Andi


你的连接超时..或者更确切地说是超时......这就是应用程序挂起的原因。您正在UI线程上打开与服务器的连接,并且它无法连接到您的数据源,因此它在您尝试连接时使应用程序挂起。我认为默认超时是3分钟...如果你等了那么久,最后应该抛出异常。如果您没有耐心,可以在调用Open方法之前设置sqlConnection对象的ConnectionTimeout属性,如果无法连接到数据源,该方法会更快超时。



如果与数据源的连接断断续续,则应考虑将连接管理移至非UI线程,以免对用户隐藏打开SQL连接的延迟。



至于为什么它无法连接,这是另一个问题,它高度依赖于你所连接的东西。不幸的是,''xyz''并没有告诉我们太多,所以我无法帮助你。 : - )

Hi, I don''t know what''s going on with visual studio 2012? when I run the following code in Visual Studio 2010, it works fine as expected. but when I run the same code in Visual Studio 2012, neither it throws exception nor it notify me about the exception. can some one tell me the matter?

string connectionString = "Data Source=xyz;Initial Catalog=abc;User ID=sa; Password=123; Integrated Security=true ";
           SqlConnection sqlConnection = new SqlConnection(connectionString);
           try
           {
               sqlConnection.Open();
               SqlCommand sqlCommand = new SqlCommand("SELECT COUNT(*) FROM tblProduct");
               sqlCommand.Connection = sqlConnection;

               int RecordCount = Convert.ToInt32(sqlCommand.ExecuteScalar());
           }
           catch (Exception ex)
           {
             MessageBox.Show(   ex.Message.ToString());
           }

解决方案

Maybe, this helps: Set in VisualStudio in the Debug --> Exceptions --> .Net Exception stop on throw.
Then debug the code again: the debugger will stop at the throw-location.
Cheers
Andi


Your connection is timing out.. or rather isn''t timing out... and that is why the app is hanging. You are opening the connection to the server on the UI thread and it isn''t able to connect to your datasource so it is making your application hang while it is trying to connect. I think the default timeout is 3 minutes... if you wait that long it should, finally, throw an exception. If you aren''t that patient, you can set the ConnectionTimeout property of the sqlConnection object before you call the Open method, then the method will timeout sooner if it can''t connect to the datasource.

If connectivity to your datasource will be intermittent, then you should consider moving the connection management to a non-UI thread so the delays in opening a SQL connection are hidden from the user.

As to why it can''t connect, that is another issue and it is highly dependent on what you are connecting to. Unfortunately, ''xyz'' doesn''t tell us much so I can''t help you there. :-)


这篇关于Visual Studio 2012例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆