3

I'm trying to update a database from my program in C#.

Here is my code which connects to the database and the attempts to update the date column in my RoomsTable table. It looks good to me but nothing happens in the database.

updateConnection = new System.Data.OleDb.OleDbConnection();
updateConnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb";
updateConnection.Open();

MessageBox.Show("Connected");

string updateCommand = "UPDATE RoomsTable SET Date Checked='9/27/2012'";
updateAdapter = new OleDbDataAdapter(updateCommand, updateConnection);

updateConnection.Close();
updateConnection.Dispose();

I don't know why it isn't working. It looks to me like everything is there.

1
  • 4
    You need to run an execute() method. Commented Sep 30, 2012 at 15:57

2 Answers 2

9

use OleDBCommand

string updateCommand = "UPDATE RoomsTable SET [Date Checked]='9/27/2012'";
updateCommand = new OleDbCommand(updateCommand, updateConnection);

updateCommand.ExecuteNonQuery();
updateConnection.Close();

maybe you could refractor the code using Using statement and parameterized the query. and column name Date Checked should be escaped with brackets.

string updateCommand = "UPDATE RoomsTable SET [Date Checked]=@checkedDate WHERE ID = @id"; // '9/27/2012'
using (OleDbConnection conn = new OleDbConnection("connectionStringHERE"))
{
    using (OleDbCommand comm = new OleDbCommand())
    {
        comm.Connection = conn;
        comm.CommandText = updateCommand;
        comm.CommandType = CommandType.Text
        comm.Parameters.AddWithValue("@checkedDate", this.dateTimePicker1.Value)
        comm.Parameters.AddWithValue("@id", row.roomID);
        try
        {
            comm.Open();
            conn.ExecuteNonQuery();
        }
        catch(OleDbException ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
}
Sign up to request clarification or add additional context in comments.

16 Comments

hmm yeah I was following a tutorial I found on the web..(i have no database experience) and it used the method I used. It was for an access databse and everything which is what im using.
I tried your method and im getting errors ("Cannot implicitly convert type 'System.Data.OleDB.OleDBCommand' to string) and ('string' does not contain a definition for 'ExecuteNonQuery'
and I do have using System.Data.OldeDb in my code at the top.
ok i got those errors solved, but now when i run it crashes and the error says there is an error in my update statement whenever .executenonquert is called
@Stonep123 where is this 9/27/2012 coming from?
|
0

you have to write this:

updateConnection = new System.Data.OleDb.OleDbConnection();
updateConnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb";

updateConnection.Open();

MessageBox.Show("Connected");

string updateCommand = "UPDATE RoomsTable SET Date Checked= '"+9/27/2012+"' ";

updateAdapter = new OleDbDataAdapter(updateCommand, updateConnection);

updateConnection.Close();
updateConnection.Dispose();

1 Comment

Always use parameters in your queries to avoid sql injection. Your updateCommand string isn't what you think it is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.