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
自动编号。
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());
}
/* 我实际上只是在我的字符串更新中的变量之前错过了 ' 标记 */