[英]c# WPF, No trouble inserting records into primary table, but can't update secondary table record
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());
}
/ *实际上我只是在字符串更新变量之前错过了''* /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.