[英]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.