質問

Im trying to use the following script to Update values in an MS Access database.

I have verified that the generated query is valid and that it works if I input it inside MS Access.

Also I verified that oledbcommand.executeNonQuery() returns the correct number of rows that should be modified.

And finally, I am receiving no errors as far as I know.

However when I check the database after I run the code there are no changes. Does Anyone have any insight on why this might be?

I have other functions that retrieve data from the database that work fine using oledbcommand. However i cant get it to modify the database.

Please not that Im aware that this script doesnt protect against injection, but that is outside the scope of this question. Im simply just trying to learn how to update the database.

void IDatabase.UpdateRecordValue(string table, string updateColumn, string updateValue, string lookUpColumn, string lookUpValue)
{
       connection.Open();
       OleDbCommand cmdUpdate = new OleDbCommand();

       string sqlQuery = "UPDATE [" + table + "] " +
                         "SET [" + table + "].[" + updateColumn + "] = '" + updateValue + "' " +
                         "WHERE [" + table + "].[" + lookUpColumn + "] = '" + lookUpValue + "';";

       cmdUpdate.CommandText = sqlQuery;
       cmdUpdate.CommandType = CommandType.Text;
       cmdUpdate.Connection = connection;

       cmdUpdate.ExecuteNonQuery();
       connection.Close();
}

EDIT: my connection string is as follows, there is no password on the database: "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};"

EDIT Here is an actual query example that Im trying to use

UPDATE [Rooms] 
SET [Rooms].[Main Space Category] = '5'
WHERE [Rooms].[Number] = '100';
役に立ちましたか?

解決

You get no error when your code executes this UPDATE, and cmdUpdate.ExecuteNonQuery() returns 1 as expected.

UPDATE [Rooms] 
SET [Rooms].[Main Space Category] = '5'
WHERE [Rooms].[Number] = '100';

Yet you report the db does not actually get updated. Investigate that point further. Try this SELECT from the same OleDB connection object you used when executing the UPDATE.

SELECT [Rooms].[Main Space Category]
FROM [Rooms]
WHERE [Rooms].[Number] = '100';

For extra measure, double-check the Data Source in the connection object's ConnectionString property.

Console.WriteLine(connection.ConnectionString)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top