这是我的代码:
public partial class Form1 : Form
{
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection cn;
SqlCommand selectcommand;
DataSet ds = new DataSet();
List<Customer> customers = new List<Customer>();
public Form1()
{
InitializeComponent();
cn = new SqlConnection(Properties.Settings.Default.exsisDBConnectionString);
}
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.TimerCallback callback = new System.Threading.TimerCallback(ProcessTimerEvent);
var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 10, 5, 0);
if (DateTime.Now < dt)
{
var timer = new System.Threading.Timer(callback, null, dt - DateTime.Now, TimeSpan.FromHours(24));
}
}
private void ProcessTimerEvent(object obj)
{
cn.Open();
selectcommand = new SqlCommand("SELECT * FROM Customer; SELECT * FROM [Transaction]; SELECT * FROM Notification; SELECT * FROM NotificationLink; SELECT * FROM Product; SELECT * FROM TransactionItem", cn);
da.SelectCommand = selectcommand;
da.TableMappings.Add("Table", "Customer");
da.TableMappings.Add("Table1", "Transaction");
da.TableMappings.Add("Table2", "Notification");
da.TableMappings.Add("Table3", "NotificationLink");
da.TableMappings.Add("Table4", "Product");
da.TableMappings.Add("Table5", "TransactionItem");
da.Fill(ds);
customers.Clear();
foreach (DataRow row in ds.Tables["Customer"].Rows)
{
Customer customer = new Customer();
customer.CustID = row["CustID"].ToString();
customer.RepID = row["RepID"].ToString();
List<Transaction> customerTransactions = new List<Transaction>();
foreach (DataRow row2 in ds.Tables["Transaction"].Rows)
{
if (row2["CustID"].ToString() == customer.CustID)
{
Transaction transaction = new Transaction();
transaction.DocNo = row2["DocNo"].ToString();
transaction.Date = Convert.ToDateTime(row2["Date"]);
customerTransactions.Add(transaction);
}
}
customer.Transactions = customerTransactions;
customers.Add(customer);
}
LastOrder();
cn.Close();
}
private void LastOrder()
{
foreach (Customer customer in customers)
{
DateTime LastOrderDate = Convert.ToDateTime(customer.Transactions.Last().Date);
TimeSpan TimeSinceLastOrder = DateTime.Now - LastOrderDate;
if (TimeSinceLastOrder.TotalDays > 30)
{
DataRow row = ds.Tables["Notification"].NewRow();
row["CustID"] = customer.CustID;
row["DateGenerated"] = Convert.ToString(DateTime.Now);
row["NotificationType"] = "Last Order";
if (ds.Tables["Notification"].Rows.Count == 0)
{
row["NotificationID"] = "1";
}
else
{
DataRow nid = (DataRow)ds.Tables["Notification"].Rows[ds.Tables["Notification"].Rows.Count - 1];
row["NotificationID"] = Convert.ToString(Convert.ToInt32(nid["NotificationID"]) + 1);
}
row["DocNo"] = customer.Transactions.Last().DocNo;
ds.Tables["Notification"].Rows.Add(row);
DataRow row2 = ds.Tables["NotificationLink"].NewRow();
if (ds.Tables["NotificationLink"].Rows.Count == 0)
{
row2["NotificationLinkID"] = "1";
}
else
{
DataRow nlid = (DataRow)ds.Tables["NotificationLink"].Rows[ds.Tables["NotificationLink"].Rows.Count - 1];
row2["NotificationLinkID"] = Convert.ToString(Convert.ToInt32(nlid["NotificationLinkID"]) + 1);
}
row2["NotificationID"] = row["NotificationID"];
row2["RepID"] = customer.RepID;
ds.Tables["NotificationLink"].Rows.Add(row2);
}
}
da.Update(ds);
}
}
当我运行它时,我没有收到任何错误,但是在运行之后数据库还没有更新。我很确定这个问题与我缺少一个InsertCommand有关,但是当我使用像这样的多个表时,我不确定如何构造一个InsertCommand。
我尝试了一下SqlCommandBuilder。我没有运气,虽然我真的不明白SqlCommandBuilder是如何工作的,所以我可能会遗漏一些东西。
另外,作为旁注,当谈到将文本添加到InsertCommand时,是否仍然要跳过添加值并让它使用您为新DataRow分配的值?因为将值分配给DataRow然后再将其分配给SQL语句似乎有点多余。