c# WPF,将记录插入主表没有问题,但无法更新辅助表记录

本文关键字:记录 更新 有问题 插入 WPF | 更新日期: 2023-09-27 18:16:01

try
{
    OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = @"myconnectionstring_blah_blah";

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "INSERT INTO StaffOrders (StaffID, OrderDate, Pants, Boots " + "VALUES (@StaffID, @OrderDate, @Pants, @Boots)";
    command.Parameters.AddWithValue("@StaffID", staffid);
    command.Parameters.AddWithValue("@OrderDate", datenow);
    command.Parameters.AddWithValue("@Pants", pant);
    command.Parameters.AddWithValue("@Boots", boot);
    command.Connection = connection;
    connection.Open();
    command.ExecuteNonQuery();
     //**********************************************************   
    if(int.Parse(bootsissued) > 0)
    {
        //Update LastBoot in StaffList with today's Date
    }
    //***********************************************************
    connection.Close();
}
catch //blah blah

我有两个访问表:

StaffList:
     ID (autonumber - pk - unique)
     StaffID (indexed unique string)
     LastBoot (Date/Time)
     ...
StaffOrders:
     ID  (autonumber - pk - unique)
     StaffID (indexed unique string) - same as above
     ... (pants, boots, etc)

我正在将记录插入StaffOrders,但我想在订购启动时将记录LastBoot(从 StaffList (更新为当前日期。

这两个表位于同一个访问数据库中。我添加订单记录没有问题,但我似乎无法UPDATE当前日期的LastBoot记录。

我必须UPDATE相应记录的唯一标识符StaffID而不是ID自动编号。

c# WPF,将记录插入主表没有问题,但无法更新辅助表记录

        DateTime datenow = DateTime.Today; 
        string myconnectionstring = @"//connectionstring";
        OleDbConnection myconnection = new OleDbConnection();
        myconnection.ConnectionString = myconnectionstring;
        try
        {
            myconnection.Open();
            ConnectedIcons();
            OleDbCommand command = new OleDbCommand();
            command.Connection = myconnection;
            string update = "UPDATE StaffList SET LastBoot = '" + datenow + "' WHERE StaffID = '" + selectedstaffid + "';";
            var accessUpdateCommand = new  OleDbCommand(update, myconnection);
            accessUpdateCommand.Parameters.AddWithValue("LastBoot", datenow);
            accessUpdateCommand.Parameters.AddWithValue("StaffID", selectedstaffid);
            command = accessUpdateCommand;
            command.ExecuteNonQuery();
            myconnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

/* 我实际上只是在我的字符串更新中的变量之前错过了 ' 标记 */