Microsoft Access ODBC driver does not execute UPDATE statements

asked 2022-03-13T05:10:38.02+00:00
w s 21 Reputation points

First of all, please understand that we are translating Japanese.

The following code does not perform UPDATE.
The updated line is returned as 0.
Please tell me the problem area and help me.

public void Save(ProcessEntity process)
{
    string insert = @"
INSERT INTO Process 
(ProcessId,CreateDate,SerialNumber,StaffName,Terminal,CompleteDate)
VALUES 
(?,?,?,?,?,?) 
";
    string update = @"
UPDATE Process
SET CreateDate = ?,
SerialNumber = ?,
StaffName = ?,
Terminal = ?,
CompleteDate = ?
WHERE ProcessId = ?
AND CreateDate =?
AND SerialNumber = ?
";
    var args = new List<OdbcParameter>
    {
        new OdbcParameter("@ProcessId",process.ProcessId.Value),
        new OdbcParameter("@CreateDate",process.CreateDate.Value),
        new OdbcParameter("@SerialNumber",process.SerialNumber.Str),
        new OdbcParameter("@StaffName",process.StaffName.Str),
        new OdbcParameter("@Terminal",process.Terminal),
        new OdbcParameter("@CompleteDate",process.CompleteDate.Value),
    };

    OdbcHelper.ExecuteProcess(insert, update, args.ToArray());
}



internal static void ExecuteProcess(string insert, string update, OdbcParameter[] paramaters)
{
    using (var connection = new OdbcConnection(ProcessConnection))
    using (var command = new OdbcCommand(update, connection))
    {
        connection.Open();
        if (paramaters != null)
        {
            command.Parameters.AddRange(paramaters);
        }
        if (command.ExecuteNonQuery() < 1)
        {
            command.CommandText = insert;
            command.ExecuteNonQuery();
        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,911 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
726 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-03-13T05:31:07.82+00:00
    Viorel 82,286 Reputation points

    Try these modifications:

    string insert = @"
    INSERT INTO Process
    (CreateDate,SerialNumber,StaffName,Terminal,CompleteDate,ProcessId)
    VALUES
    (?,?,?,?,?,?)
    ";
    . . .
    var args = new List<OdbcParameter>
     {
         new OdbcParameter("@CreateDate",process.CreateDate.Value),
         new OdbcParameter("@SerialNumber",process.SerialNumber.Str),
         new OdbcParameter("@StaffName",process.StaffName.Str),
         new OdbcParameter("@Terminal",process.Terminal),
         new OdbcParameter("@CompleteDate",process.CompleteDate.Value),
         new OdbcParameter("@ProcessId",process.ProcessId.Value),
     };
    

0 additional answers

Sort by: Most helpful