我有一个 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