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);
}
}
}
錯誤消息的自我解釋......你沒有定義一個InsertCommand。添加類似'dataAdapter.InsertCommand = connection.CreateCommand()...',或者,我的建議,使用Visual STudio嚮導生成85%的數據訪問代碼 –
因此,上面的問題是錯誤的? – user310291
我只有一個沒有數據集的數據表,我不想讓嚮導生成完整的數據集,它有點習慣的過度的 – user310291