C# OLEDBDATAADAPTER 配合 commandbuilder 连接数据库和dataset时注意事项

数据库如果是ACCESS

//select

m_adapter = new OleDbDataAdapter();
m_command = new OleDbCommand(SQL, m_conn);
m_adapter.SelectCommand = m_command;


Dateset Ds = new DataSet();
Ds.Tables.Add(subtableName);
m_adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
m_adapter.Fill(Ds, subtableName);

//使用commandbuilder自动生成select语句,以后直接修改dataTable即可,不用自己写update,insert,delete的SQL,
m_commandbuilder = new OleDbCommandBuilder(m_adapter);
m_commandbuilder.QuotePrefix = "["; //ACCESS数据库字串是 select * from [tablename] .....
m_commandbuilder.QuoteSuffix = "]";

//update

m_command = new OleDbCommand(SQL, m_conn);
m_adapter.SelectCommand = m_command;

foreach(DataRow row in ds.tables[0].Rows) //我之前就是没把dt的每一行给endedit,所以一直没法更新
row.EndEdit();

int r = m_adapter.Update(ds, tablename); //返回成功更新的行数
ds.AcceptChanges();

你可能感兴趣的:(sql,C++,c,C#,Access)