C#应用程序连不上数据导致"应用没反应"卡顿现象怎么解决?

发布于 2021-11-26 13:11:05 字数 1836 浏览 734 评论 2

考虑到有可能出现网络不稳定或者中断的情况下,数据库连接失败,所以当程序运行的时候我关掉了SQL Server2005数据库服务,导致程序出现”程序没反应“的卡顿现象,这严重影响了使用,怎么解决这个问题呢

在catch中没有进行其他处理,只是进行了日志记录

如下是示例:

public static bool delStation(string[] ids)
        {
            SqlConnection sqlConnection = null;
            SqlCommand sqlCommand = null;
            bool result = false;
            try
            {
                sqlConnection = new SqlConnection(Operation.connStr);
                sqlConnection.Open();
                sqlCommand = sqlConnection.CreateCommand();
                StringBuilder stringBuilder = new StringBuilder("delete from zzbj_station where station_num in ('");
                for (int i = 0; i < ids.Length; i++)
                {
                    string value = ids[i];
                    stringBuilder.Append(value);
                    stringBuilder.Append("','");
                }
                stringBuilder.Append(")");
                stringBuilder.Remove(stringBuilder.ToString().LastIndexOf(","), 2);
                sqlCommand.CommandText = stringBuilder.ToString();
                int num = sqlCommand.ExecuteNonQuery();
                if (num > 0)
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                MyLog.WriteLog("AlarmMonitor1.Operation.cs中612行 " + e.ToString());
            }
            finally
            {
                if (sqlCommand != null)
                {
                    sqlCommand.Dispose();
                }
                if (sqlConnection != null)
                {
                    sqlConnection.Close();
                }
            }
            return result;
        }



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2021-11-29 02:20:55

的确是跟数据库超时时间有关,设定的timeout为30s,改成了5s,感觉5s这个值还是有点不靠谱,先跑跑,看看会出现什么状况

甜柠檬 2021-11-28 19:39:32

timeout  ADO.net 有这个设置

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文