1

我有一个 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.");
        } 
    }

数据库查询自己:

    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.7600.2.0.0.305.9

区域设置 ID:1033

附加信息1:a7aa

附加信息 2:a7aa91f17ea749d42a4de3b390fa5b3d

附加信息 3:a7aa

附加信息 4:a7aa91f17ea749d42a4de3b390fa5b3d

4

3 回答 3

3

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

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

只需在我的 Linq to SQL 查询中添加“where number != null”即可解决我的问题。

于 2014-04-02T16:16:55.033 回答
2

这段代码有点时髦。最大的问题是不使用动态列表。然后你就不需要两个数据库调用了。您不需要计数等。您还应该使用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();
}
于 2013-08-21T15:30:50.020 回答
0

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

绝望中,我翻遍了整个数据库,寻找可以摆脱它的东西。数据库中有一个重复的条目,我猜当 Total Rows(由 CountRows 返回)与 Distinct 行数不匹配时它会损坏。

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

于 2013-08-21T15:19:11.327 回答