I'm working with a Microsoft Access Database in my Winforms project. I'm using OLEDB to connect. I have the following table I'm trying to update:
TotalLogins
--------------------
ID AutoNumber
TokenNo Text
UserID Number
FromDate DateTime //Contains datetime of login
ToDate DateTime //Contains datetime of logout
How do I write C# code that updates this table开发者_如何转开发 with all the required information?
Try this code as example
OleDbConnection cn = new OleDbConnection(conString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "UPDATE TotalLogins SET TokenNo=@TokenNo, UserID=@UserID, FromDate=@FromDate, ToDate=@ToDate WHERE ID=@ID";
cmd.Parameters.AddWithValue("@TokenNo", varTokenNo);
cmd.Parameters.AddWithValue("@UserID", varUserID);
cmd.Parameters.AddWithValue("@FromDate", varFromDate);
cmd.Parameters.AddWithValue("@ToDate", varToDate);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
精彩评论