【问题标题】:C# Winform application crash on SqlConnectionSqlConnection 上的 C# Winform 应用程序崩溃
【发布时间】:2019-05-09 20:45:12
【问题描述】:

我有一个 Winform 程序,它有很多与本地 MDF 数据库交互的 SqlConnection 函数。

这就是我创建连接字符串的方式:

String dbPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Bot\DB\BotDB.mdf";
String con = String.Format("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;", dbPath);
return con;

这是我对数据库进行的许多调用之一:

public bool InsertNewUserToTable(String username)
{
    using (SqlConnection con = new SqlConnection(DataBase.GetConString()))
    {
        con.Open();

        String query = @"INSERT INTO dbo.Users(username) VALUES(@username);";

        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@username", username);

            int count = cmd.ExecuteNonQuery();

            con.Close();

            return (count == 0) ? false : true;
        }
    }
}

我有更多这样的方法,可以从数据库中删除、更新、插入数据。

以这种方式访问​​数据库的多线程程序。

问题是有时我会遇到崩溃错误(在发布模式下):

Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01: Bot.exe
  Problem Signature 02: 1.0.0.0
  Problem Signature 03: 5cbce361
  Problem Signature 04: System.Data
  Problem Signature 05: 4.7.2623.0
  Problem Signature 06: 5a1f67d8
  Problem Signature 07: 1a55
  Problem Signature 08: 5e
  Problem Signature 09: System.Data.SqlClient.Sql
  OS Version:   6.3.9600.2.0.0.272.7
  Locale ID:    1033
  Additional Information 1: 4e4b
  Additional Information 2: 4e4be395a3a959f2a72f71ab9c9204ab
  Additional Information 3: 783e
  Additional Information 4: 783ea04fb900812a4241ad9aeb1b45b6

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=280262

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

我试图找到一种方法来使用以下信息找到特定的异常: https://stackoverflow.com/a/4053325/679099

但我找不到任何使我的应用程序崩溃的具体问题。

知道可能是什么问题吗?我还缺少什么?

【问题讨论】:

  • 不能记录异常吗?那会让生活更轻松???? ... ps,更多信息可能会写入事件日志。
  • @Stefan 这是一个包含 50 多个连接数据库的方法的代码,我什至不知道是哪一个导致了这个问题。我在哪里可以找到这个日志?
  • 您可以制作一个通用的 uncautch exeption 处理程序。至于事件日志,如果它在 Windows 上,就会是这样的howto-connect.com/event-viewer-on-windows-10

标签: c# sql winforms mdf


【解决方案1】:

尝试定义一个私有的SqlConnection,并将这段代码放在InitializeComponent()下;

con = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;User Instance=True;MultipleActiveResultSets=True;");
con.Open();

此连接适用于打开的多个连接,因为您有 MARS=True;

那么对于SqlCommand,你只需要这样:

SqlCommand cmd = new SqlCommand("query", con);

SqlConnection 的最终代码必须如下所示:

private SqlConnection con;
public Form1()
{
    InitializeComponent();
    con = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;User Instance=True;MultipleActiveResultSets=True;");
    con.Open();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多