这个问题在这里已有答案:
我正在使用SQLite和SQLite-Net Wrapper for WinRT app . 其他平台可能有SQLite,但实现可能会有所不同,例如使用SQLite-Net api .
如何在插入SQLite后立即获取最后一行ID?谢谢
using (var db = new SQLite.SQLiteConnection(DBPath))
{
var newOrder = new SalesOrder()
{
CustId = g_intCustId,
Customer_No = txtBlkCustomer.Text.Trim(),
Order_Date = DateTime.Today
};
db.Insert(newOrder);
}
--1--- Update : I am using SQLite-Net Wrapper. I am not using SQLite -WInRT
I get the following error :
The type arguments for method 'SQLite.SQLiteConnection.ExecuteScalar(string, params object[])'
cannot be inferred from the usage. Try specifying the type arguments explicitly.
db.Insert(newOrder);
var key = db.ExecuteScalar("SELECT last_insert_rowid()");
---2-- Update
This is the class :
My problem is : How to get the SId immediately after inserting a record using above code.
class SalesOrder
{
[PrimaryKey, AutoIncrement]
public int SId { get; set; }
public int CustId { get; set; }
public string Customer_No { get; set; }
public DateTime Order_Date { get; set; }
}
2 回答
在SQLite-net中,
Insert
方法返回插入的行数(SQLite.cs) . 因此,如果您希望它返回最后一行ID,您可以更新它以执行此操作 .目前的实施 .
更新实施 .
你的连接上有ExecuteScalar方法吗?然后用