2

SQL サーバーがあり、それをアプリのメイン フォームに表示したいと考えています。いくつかのガイドに従って、dataGrid と SQL サーバー テーブルをリンクする試みに成功しました。

問題は、データグリッドを更新し、SQL サーバーからテーブルを再度同期/リロードする場合です。

updateDatadrid()データベースで行を追加/編集するたびに関数を呼び出すイベント ハンドラーがあります。

アプリを最初に起動したとき、この関数を呼び出して動作していますが、その後、そのハンドラーで再度呼び出すと、いくつかのエラーが発生します。

クロススレッド操作が無効です: コントロール '' は、それが作成されたスレッド以外のスレッドからアクセスされました。

関数の外 (グローバル変数):

SqlConnection sConDataGrid; 
DataSet dbDataSet;
SqlDataAdapter da;
BindingSource dbBind;
string sqlCommand;

EDIT - I forgot to add :
DataGridView _dbView;

これは私のコードです:

private void updateDataGrid()
{
        using (sConDataGrid = new SqlConnection("Data Source=" + SettingsForm.getAddress + ";Initial Catalog=" + SettingsForm.getDatabase + ";Integrated Security=False;User Id=" + SettingsForm.getUser + ";Password=" + SettingsForm.getPassword + ";Connect Timeout=0;"))
        {
            sConDataGrid.Open();
            sqlCommand = "select top 200 * FROM cstPackages order by _dateTime desc"; //reading the db from end to start   
            using (da = new SqlDataAdapter(sqlCommand, sConDataGrid))
            {
                da.Fill(dbDataSet, "cstPackages");
                    dbBind = new BindingSource(dbDataSet, "cstPackages");
                    _dbView.DataSource = dbBind;
                sConDataGrid.Close();
            }
        }
}

da.Fill()私はそれを試してみましたが、この関数を初めて呼び出した後から関数のみを呼び出しましたが、データグリッドは更新されませんでした。データセットを再度埋める前にデータセットをクリアしようとしましたが、プログラムがクラッシュしました。やってみました_dbView.Refresh()が、うまくいきませんでした..

編集 ボタンを作成し、「クリック」イベントハンドラーを配置して、このイベントハンドラーから関数を呼び出しているのはなぜですか?それは機能しています! SerialPort dataReceivedイベント ハンドラからこの関数を呼び出すと、このエラーが発生します。違いはなんですか?イベント ハンドラーを適切な方法で作成していないのではないかと考えたのでSerialPort、デザイナーからコントロールをドラッグし、イベントをダブルクリックしましたが、同じことが起こりました。

タイマーを作成して有効にし、関数の最後に を呼び出しstop()、シリアル受信で に設定しよstart()うとしましたが、ハンドラーで行うことはすべてSerialPortプログラムの「外側」に似ています。タイマーを開始しません。

4

3 に答える 3

1

解決済みこれを読んだ後: http://msdn.microsoft.com/en-us/library/ms171728%28VS.80%29.aspx Invokes 使用してみましたが、うまくいきました!

new code:
        delegate void SetDataGridCallback();

        private void updateDataGrid()
        {
            if (this._dbView.InvokeRequired)
            {
                SetDataGridCallback d = new SetDataGridCallback(updateDataGrid);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                using (sCon2 = new SqlConnection("Data Source=" + SettingsForm.getAddress + ";Initial Catalog=" + SettingsForm.getDatabase + ";Integrated Security=False;User Id=" + SettingsForm.getUser + ";Password=" + SettingsForm.getPassword + ";Connect Timeout=0;"))
                {
                    sCon2.Open();
                    string sqlCommand = "select top 200 * FROM cstPackages order by _dateTime desc"; //reading the db from end to start   
                    using (da = new SqlDataAdapter(sqlCommand, sCon2))
                    {
                        dbDataSet.Clear();
                        da.Fill(dbDataSet, "cstPackages");
                        BindingSource dbBind = new BindingSource(dbDataSet, "cstPackages");
                        _dbView.DataSource = dbBind;
                        _dbView.Refresh();
                         sCon2.Close();
                    }
                }
            }
        }
于 2012-12-27T19:50:11.707 に答える
0

これを試して :

     private void fill_grid()
    {

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Your sql command ";

        cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = your_control.Text;

        cmd.CommandType = CommandType.Text;
        cmd.Connection = this.sqlConnection1;
        this.sqlConnection1.Open();


        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adpt.Fill(ds);


        your_grid.DataSource = ds;


        this.sqlConnection1.Close();

        this.your_grid.DataBind();



    }

その後、グリッドを「更新」する必要があるときはいつでも、このメソッドを呼び出すだけです。

于 2012-12-27T00:06:06.477 に答える
0

理由はたくさんあると思います。1 : キャッシュがクリアされていません 2 : コンピューターの動作が遅い thd : 更新が実際には更新されていません。4 : 時間の遅れがあるかもしれません

最初にデータベースで「select sql」を実行し、実際にコミットすることを確認してください または更新するボタンを作成します。事業を再開させてください。

于 2012-12-27T03:49:26.047 に答える