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.");
        } 
    }

そして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

OS バージョン: 6.1.7600.2.0.0.305.9

ロケール ID: 1033

追加情報 1: a7aa

追加情報 2: a7aa91f17ea749d42a4de3b390fa5b3d

追加情報 3: a7aa

追加情報 4: a7aa91f17ea749d42a4de3b390fa5b3d

4

3 に答える 3

2

このコードは少しファンキーです。最大の問題は、動的リストを使用していないことです。そうすれば、2 つの 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();
}
于 2013-08-21T15:30:50.020 に答える
0

さて、これは最もばかげたものです:

必死になって、私はデータベース全体を調べて、それを台無しにするものを探しました. DB に重複したエントリがありましたが、Total Rows (CountRows によって返される) が個別の行数と一致しなかったときに壊れたと思います。

本当にばかげていますが、重複したエントリを削除した後は機能します。

于 2013-08-21T15:19:11.327 に答える