[英]compare sql insert date and c# date time
我正在交易網站上。 創建訂閱后,修改的日期將插入表中。 我要完成的工作是唯一標識該記錄。 這是我正在嘗試的方法:
DateTime currentTime = DateTime.Now;
Debug.WriteLine("Current Timestamp is: " + currentTime.ToString("yyyy-MM-dd HH:mm:ss:fff"));
sCmd.CommandText = "SELECT distinct userID FROM table WHERE Report_OID = @reportID AND ModifiedDate = @currentTime";
sCmd.Parameters.AddWithValue("@reportID", reportID);
sCmd.Parameters.AddWithValue("@currentTime", currentTime.ToString("yyyy-MM-dd HH:mm:ss:fff"));
Output:
Current Timestamp is: 2016-06-09 10:19:16:586
Modified Date is: 2016-06-09 10:19:16.553
試過這個:
sCmd.CommandText = "SELECT LAST_INSERT_ID();";
IDataReader reader = sCmd.ExecuteReader();
while (reader != null && reader.Read())
{
long lastInsertedID = reader.GetInt64(0);
Debug.WriteLine("return id is : " + lastInsertedID);
}
Got this error:
'LAST_INSERT_ID' is not a recognized built-in function name.
This error may have happened due to the createSubscription method call to create a subscription in the reporting service webservice which handles the insert in the database. I do not have insert statements in my code.
這將失敗,因為毫秒不會相同。 我要使用毫秒的原因是,如果兩個用戶大約同時插入一個預訂,則可以防止多個用戶標識。
我想弄清楚的是比較這兩個日期,這樣我就可以准確地獲取我的交易的用戶ID,而不必擔心如果同時插入兩個交易,就可以獲取其他用戶的ID。 這可能嗎? 我是否試圖以錯誤的方式解決此問題。 任何幫助將不勝感激。 謝謝。
原諒我的偽代碼(我不經常使用MySQL),但是執行此操作的正確方法是修改插入內容以返回語句插入的最后一條記錄的標識,而不是嘗試使用時間戳進行匹配和userIds。 該代碼將更簡單,更可靠。 這是在MySQL中使用LAST_INSERT_ID()
函數(MS SQL的SCOPE_IDENTITY()
函數的近似值)完成的。
例如,您的插入內容(假設表中有一個名為“ FooID”的標識列,可能看起來像這樣:
INSERT INTO Foo (Bar) VALUES ('Biz');
SELECT LAST_INSERT_ID();
如果新記錄的FooID為148,則您的語句將插入該記錄並返回148,此時您可以:
SELECT Bar FROM Foo WHERE FooID = 148;
那應該可以完成您想要做的事情。
只需使用輸入之一來查找記錄。 你有幾個。
感謝@Paparazzi,我創建了另一個select語句,以通過使用電子郵件地址並在調用createSubscription方法之前創建一個datetime變量來獲取最新的訂閱,這樣我就可以獲取最新的插入ID及其現在的工作。
DateTime subscriptionCreateTime = DateTime.Now;
CreateSubscription(); //Create a new ssrs subscription
//SQL connection here
sCmd.CommandText = "SELECT distinct ownerID FROM db.table WHERE Report_ID = @reportID AND ModifiedDate >= @currentTimeStamp AND Description like @emailAddress";
sCmd.Parameters.AddWithValue("@reportID", gRptID);
sCmd.Parameters.AddWithValue("@emailAddress", "%"+tbTO.Text+"%");
sCmd.Parameters.AddWithValue("@currentTimeStamp", subscriptionCreatedTime);
SqlDataReader reader = sCmd.ExecuteReader();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.