2017-01-13 5 views
0

私はVisual Studio 2017でWindowsフォームを作成しました。ちょうどDataGridViewです。デスクトップ上のアクセスデータベースに接続されている 私のプログラムを実行すると、私はDataGridViewにデータを挿入したり、データを更新したりすることができます。しかし、私のアクセスファイルでは何も変更されません。私はそれがちょうどプログラム中にローカルdatasetを変更すると思う。DataGridViewでアクセスデータベースを変更する

private string rq_sql; 
private string cnx; 

private System.Data.OleDb.OleDbConnection oCnx; 
private System.Data.OleDb.OleDbCommand oCmd; 
private System.Data.DataSet oDS; 

private void btn_insert_Click(object sender, EventArgs e) // insert content of textboxs when i click on the button 
{ 
     this.actual_name = this.textBox2.Text; 
     this.actual_phone = this.textBox1.Text; 

     bg_matou = "INSERT INTO Table1(Telphone, Prenom) VALUES ('"+actual_phone+"','"+actual_name+"')"; 
     oCmd = new OleDbCommand(rq_sql, oCnx); 
     oCnx.Open(); 
     oCmd.ExecuteNonQuery(); 
     oCnx.Close(); 
     this.table1TableAdapter.Fill(this.database1DataSet2.Table1); 
} 

私のdatabseはよく更新されますが、私のプログラムではローカルで、ファイル内で修正する解決策が見つかりません。助けをありがとうございました。

私はOleDbCommandBuilderについて何かを見つけましたが、それはVB .NETにあり、これについて実際にはわかりません。 C#で

答えて

1

OleDbDataAdapterおよの例に

using Microsoft.VisualBasic; 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data; 
using System.Diagnostics; 
using System.Data.OleDb; 

public class frmDatabase 
{ 
OleDbConnection con = new OleDbConnection(); 
DataSet ds = new DataSet(); 
DataTable dt = new DataTable(); 
OleDbDataAdapter da = new OleDbDataAdapter(); 

private void frmProject_Load(object sender, EventArgs e) 
{ 
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\xxx\\xxx\\xxx.mdb"; 
    con.Open(); 
    ds.Tables.Add(dt); 
    da = new OleDbDataAdapter("Select * from table", con); 
    da.Fill(dt); 
    dgvDetails.DataSource = dt.DefaultView; 
    con.Close(); 
} 


private void cmdUpdate_Click(object sender, EventArgs e) 
{ 
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\xxx\\xxx\\xxx.mdb"; 
    con.Open(); 
    ds.Tables.Add(dt); 
    da = new OleDbDataAdapter("Select * from table", con); 
    da.Update(dt); 
    con.Close(); 

} 
public frmDatabase() 
{ 
    Load += frmProject_Load; 
} 
} 
+0

あなたexemple上のクエリを選択するしかありません。 INSERT/UPDATE/DELETEが動作し、アクセスファイルを変更しますか? – saperlipopette

+0

これは、https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.insertcommand(v=vs.110).aspxで実行できます。 – Svekke

関連する問題