2011-07-13 43 views
0

在形式接近我所说的更新,但我得到的错误信息:更新需要的InsertCommand,而我从这个啧啧http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database为什么我用适配器更新不起作用

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace datatablemsaccess 
{ 
    partial class Form1 
    { 
     OleDbDataAdapter dataAdapter; 

     private string getSQL() { 
      string sql = "SELECT * from tPerson"; 
      return sql; 
     } 

     private string getConnectionString() { 

      string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"App_Data\person.mdb"; 
      return connectionString; 
     } 

     private DataTable getDataTable(string sql) 
     { 
      DataTable dataTable; 

      string connectionString = getConnectionString(); 

      using (dataAdapter = new OleDbDataAdapter(sql, connectionString)) 
      { 
       dataTable = new DataTable(); 
       dataAdapter.Fill(dataTable); 
      } 

      return dataTable; 
     } 
    } 
} 



using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.OleDb; 


namespace datatablemsaccess 
{ 
    public partial class Form1 : Form 
    { 
     BindingSource bindingSource; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      string sql = getSQL(); 

      bindingSource = new BindingSource(); 

      bindingSource.DataSource = getDataTable(sql); 
      dataGridView1.DataSource = bindingSource; 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      dataAdapter.Update((DataTable)bindingSource.DataSource); 
     } 
    } 
} 
+1

错误消息的自我解释......你没有定义一个InsertCommand。添加类似'dataAdapter.InsertCommand = connection.CreateCommand()...',或者,我的建议,使用Visual STudio向导生成85%的数据访问代码 –

+0

因此,上面的问题是错误的? – user310291

+0

我只有一个没有数据集的数据表,我不想让向导生成完整的数据集,它有点习惯的过度的 – user310291

回答

相关问题