自动完成会导致崩溃

本文关键字:崩溃 | 更新日期: 2023-09-27 18:28:33

我有一个Windows窗体应用程序,它有几个字段,包括"公司"字段和"联系人"字段。当您在公司字段中键入项目并点击按钮时,它会查询SQL数据库,在"联系人"字段中填写该公司的联系人信息。我在"公司"字段中包含了非常基本的自动完成,主要是为了方便。

问题是,当我加载表单时,只要我在"公司"字段中键入任何内容,程序就会崩溃。击键时没有其他调用,我将其缩小到导致问题的自动完成。

管理这一切的代码如下:

    public void GetRowCount()
    {
        try
        {
            _DbRows = db.CountRows();
            tContact.Text = _DbRows.ToString();
        }
        catch (Exception tEx)
        {
            MessageBox.Show("Exception in GetRowCount. Exception: " + tEx.Message);
        }
    }
    private void GetCustomerList()
    {
        String customerQuery = "SELECT DISTINCT Name FROM Customers";
        try
        {
            _CustomerList = db.ReturnCustomers(customerQuery, _DbRows);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    public void PopulateAutofillList()
    {
        try
        {
            tCompany.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tCompany.AutoCompleteCustomSource.AddRange(_CustomerList);
            MessageBox.Show(_CustomerList.Length.ToString());
            tCompany.AutoCompleteMode = AutoCompleteMode.Append;
        }
        catch (Exception tEx)
        {
            MessageBox.Show("Exception On Autocomplete. Exception: " + tEx.Message);
        }
    }

这些都是在OnLoad方法中单独调用的,如下所示:

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            GetRowCount();
            GetCustomerList();
            PopulateAutofillList();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Initial Connection to the Database Failed.");
        } 
    }

DB自己查询:

    public String[] ReturnCustomers(string sqlQuery, int size)
    {
        createConnectionString();
        StreamWriter file = new StreamWriter("dbCustomerList");
        int i = 0;
        String[] results = new String[size];
        SqlConnection myConnection = new SqlConnection(_ConnectionString); 
        {
            myConnection.Open();
            SqlCommand cmd = new SqlCommand(sqlQuery, myConnection); 
            {
                SqlDataReader reader;
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(0));
                    results[i] = reader.GetString(0);
                    //file.WriteLine(i ": " + results[i]);
                    i++;
                }     
                return results;
            }
        }
    }
    public int CountRows()
    {
        createConnectionString();
        int rows;
        SqlConnection myConnection = new SqlConnection(_ConnectionString); 
        {
            myConnection.Open();
            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Customers;", myConnection); 
            rows = Convert.ToInt32(cmd.ExecuteScalar());
            Console.Write("Row Count: " + rows);
        }
        return rows;
    }

我不完全确定是什么坏了。一路上我所有的小支票都表明事情是对的。为了测试,我在SQLite上运行了所有这些,一切都很好。当我把它移到SQL时,它坏了。

--

编辑:

Windows Small Business Server 2011给出的完全例外:

问题签名:问题事件名称:APPCRASH

应用程序名称:SSLP.exe

应用程序版本:1.0.0.0

应用程序时间戳:5213d1b8

故障模块名称:shell32.dll

故障模块版本:6.1.7600.17038

故障模块时间戳:4fd2d370

异常代码:c0000005

异常偏移:000ac2c5

操作系统版本:6.1.760.2.0.0.305.9

区域设置ID:1033

附加信息1:a7aa

附加信息2:a7aa91f17ea749d42a4de390fa5b3d

附加信息3:a7aa

附加信息4:a7aa91f17ea749d42a4de390fa5b3d

自动完成会导致崩溃

我遇到了类似的问题——从数据库中获取一个数字列表用作自动完成源。特别是,我使用了Linq-to-SQL,并返回了一个不同的列表。

这个不同列表中的一个值为null,当我在带有自动完成源的文本框中输入一个值时,这导致我的程序崩溃。但是,找不到捕捉任何异常的方法,所以调试它有点麻烦。

只需在我的Linq-to-SQL查询中添加一个"where number!=null"就解决了这个问题。

这段代码非常时髦。最大的问题是不使用动态列表。那么您就不需要两个DB调用了。您不需要计数等。您还应该对这些对象使用using。像这样:

public String[] ReturnCustomers(string sqlQuery, int size)
{
    createConnectionString();
    StreamWriter file = new StreamWriter("dbCustomerList");
    List<string> results = new List<string>();
    using (SqlConnection myConnection = new SqlConnection(_ConnectionString))
    {
        myConnection.Open();
        using(SqlCommand cmd = new SqlCommand(sqlQuery, myConnection))
        {
            using(SqlDataReader reader = cmd.ExecuteReader())
            {
              while (reader.Read())
              {
                Console.WriteLine(reader.GetString(0));
                results.Add(reader.GetString(0));
              }     
            }
        }
        myConnection.Close();
    }
    return results.ToArray();
}

好吧,这是最愚蠢的事情:

在绝望中,我浏览了整个数据库,寻找能让它崩溃的东西。数据库中有一个重复的条目,我猜它是在Total Rows(由CountRows返回)与Distinct行数不匹配时崩溃的。

真的很笨,但在删除重复条目后,它确实有效。