简体   繁体   中英

Database won't update a table using OleDbDataAdapter

在此处输入图片说明

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        adapter = new OleDbDataAdapter("SELECT * FROM EMP", "Provider = OraOLEDB.Oracle;Driver=OraOLEDB.Oracle;Data Source=192.168.1.25/ORA8I;User id=INTERNSHIP;Password=123");
        cmd = new OleDbCommandBuilder(adapter);

        adapter.Update(ds, "EMP");
        MessageBox.Show("information updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   catch
   {
        MessageBox.Show("There is nothing to update");
   }
}

Note: using oracle 11g databse. Dataset ds is set during form loading as Dataset ds = new Dataset();

Error: Dynamic sql generation against select command.

But i have set phno as my primary key .

If you want to initialize a dataset from the database you should use the Fill method of the OleDbDataAdapter class instead of the Update method

adapter.Fill(ds);

The error tells that there is a need to specify the fields of the table you want to update. The easiest way is to generate the update statement using the OleDbCommandBuilder class.

 adapter = new OleDbDataAdapter("SELECT * FROM EMP", "...");
 cmd = new OleDbCommandBuilder(adapter);
 adapter.UpdateCommand = cmd.GetUpdateCommand();
 adapter.Update(ds, "EMP");

The builder will generate the update statement and specify all fields.

Don't forget to dispose the resources you use (connection, adapter, command, etc.). Just call the Dispose method in the end:

adapter.Dispose();

or use the using keyword

using (adapter = new OleDbDataAdapter("...", "..."))
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM