2017-05-18 27 views
-1

最後の4時間私のことを掴んでいる問題です。私のデータセットは変更を取得しますが、データベースはdoesntです。ただ、「グローバル」VARとして周りにありC#データベースがデータセットから更新されていません。 SQLCommandBuilder

スタッフ:

DatabaseConnection objConnect;   
    string conString; 
    DataSet ds; 
    int MaxRows; 

マイフォームの負荷は、DB接続を初期化します

  objConnect = new DatabaseConnection(); 
      conString = Properties.Settings.Default.memoDbConnectionString; 
      objConnect.connection_string = conString; 

私は私のデータセットを変更し、私を呼び出して、このボタンを持っていますデータベースの更新。私はDatabaseConnectionクラスで使用

private void button5_Click(object sender, EventArgs e) 
    { 
     // 

     objConnect.Sql = Properties.Settings.Default.userSQL; 
     ds = objConnect.GetConnection; 
     MaxRows = ds.Tables[0].Rows.Count; 

     for (int i = 0; i < MaxRows; i++) 
     { 
      DataRow dRow = ds.Tables[0].Rows[i]; 
      if (nick == dRow.ItemArray[0].ToString()) 
       if (textBox4.Text == textBox5.Text) 
        if (textBox3.Text == dRow.ItemArray[1].ToString()) 
        { 
         ds.Tables[0].Rows[i][1] = textBox5.Text; 
         //this is the interesting bit 
         DataSet changes = ds.GetChanges(); 

          objConnect.UpdateDatabase(changes); 

         //----- 
         MessageBox.Show(nick); 
         MessageBox.Show(dRow[1].ToString()); 

         break; 
        } 
        else MessageBox.Show("Old password is wrong."); 
       else MessageBox.Show("Pass confirmation does not correspond."); 
        } 
     } 

機能は:

public void UpdateDatabase(System.Data.DataSet ds) 
    { 
     System.Data.DataSet changes = ds.GetChanges(); 
     System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1); 
     //stuff thats suppsed to help 
     da_1.UpdateCommand = cb.GetUpdateCommand(); 
     //---- 
      da_1.Update(ds.Tables[0]); 
     ds.AcceptChanges(); 
    } 

da_1は、私は関数の外で宣言しますが、この1つだけ前に、関数の中にそれを属性SqlDataAdapterオブジェクトです。

+0

私はちょうど1つのデータベースから読み取り、何らかの理由で別のデータベースに書き込むことがわかりました.my接続文字列は次のとおりです:Visual Studio 2015 \ Projects \ memo \ memo \ Resources \ memoDb.mdf –

+0

ソリューションエクスプローラで、無視され、実際にはパスにあるものです。 –

答えて

0

この回答:thisポストから

Ok, I'm going to take a guess here. Is the PacjenciDB.sdf included 
into Visual Studio project by any chance? Do you have the property" Copy 
to output folder" set to "Always" or something similar? It seems 
that every time you do a build you could be overwriting 
your output folder database file. Try putting the database in a folder 
that is not inside VS project. 

BTW, your code is OK. 

が助けてくれました。 デバッグフォルダからプログラムを起動すると、問題はなく、問題を見つけるのに役立ちます。問題は、dbをプロジェクトにデータソースとして追加しただけでなく、その接続文字列とそれぞれの類似点を作成します。データソースから削除し、ソリューションエクスプローラーの正しいフォルダーから新規に追加すると、私のために修正されました。