C# 我需要这样做:在表中插入一条记录,并立即获取该记录的表主键。上面的代码,我使用db.Insert(newOrder),然后Int Id=newOrder.SId,其中SId是该表中的主键,这个方法正确吗?如果使用我的解决方案&var Id=db.Inser

C# 我需要这样做:在表中插入一条记录,并立即获取该记录的表主键。上面的代码,我使用db.Insert(newOrder),然后Int Id=newOrder.SId,其中SId是该表中的主键,这个方法正确吗?如果使用我的解决方案&var Id=db.Inser,c#,sqlite,windows-phone-7,winrt-xaml,C#,Sqlite,Windows Phone 7,Winrt Xaml,我需要这样做:在表中插入一条记录,并立即获取该记录的表主键。上面的代码,我使用db.Insert(newOrder),然后Int Id=newOrder.SId,其中SId是该表中的主键,这个方法正确吗?如果使用我的解决方案&var Id=db.Insert(newOrder)那么ID将是SId,这是您的主键和自动递增列。我认为var ID=db.Insert(newOrder)中的ID不是主键,因为它总是返回1。我尝试了这个Int Id=newOrder.SId,其中Id在每次创建新记录时都是


我需要这样做:在表中插入一条记录,并立即获取该记录的表主键。上面的代码,我使用db.Insert(newOrder),然后Int Id=newOrder.SId,其中SId是该表中的主键,这个方法正确吗?如果使用我的解决方案&
var Id=db.Insert(newOrder)
那么ID将是
SId
,这是您的主键和自动递增列。我认为var ID=db.Insert(newOrder)中的ID不是主键,因为它总是返回1。我尝试了这个Int Id=newOrder.SId,其中Id在每次创建新记录时都是不同的。你能证实这一点吗?谢谢:)您是否根据我的解决方案更改了SQLite.cs?可以使用此Int Id=newOrder.SId。这与您的解决方案有任何不同。 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; } }
var key = db.ExecuteScalar<int>("SELECT last_insert_rowid()");
public int Insert (object obj, string extra, Type objType)
{
    if (obj == null || objType == null) {
        return 0;
    }


    var map = GetMapping (objType);

    #if NETFX_CORE
    if (map.PK != null && map.PK.IsAutoGuid)
    {
        // no GetProperty so search our way up the inheritance chain till we find it
        PropertyInfo prop;
        while (objType != null)
        {
            var info = objType.GetTypeInfo();
            prop = info.GetDeclaredProperty(map.PK.PropertyName);
            if (prop != null) 
            {
                if (prop.GetValue(obj, null).Equals(Guid.Empty))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                break; 
            }

            objType = info.BaseType;
        }
    }
    #else
    if (map.PK != null && map.PK.IsAutoGuid) {
        var prop = objType.GetProperty(map.PK.PropertyName);
        if (prop != null) {
            if (prop.GetValue(obj, null).Equals(Guid.Empty)) {
                prop.SetValue(obj, Guid.NewGuid(), null);
            }
        }
    }
    #endif


    var replacing = string.Compare (extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0;

    var cols = replacing ? map.InsertOrReplaceColumns : map.InsertColumns;
    var vals = new object[cols.Length];
    for (var i = 0; i < vals.Length; i++) {
        vals [i] = cols [i].GetValue (obj);
    }

    var insertCmd = map.GetInsertCommand (this, extra);
    var count = insertCmd.ExecuteNonQuery (vals);

    if (map.HasAutoIncPK)
    {
        var id = SQLite3.LastInsertRowid (Handle);
        map.SetAutoIncPK (obj, id);
    }

    return count;
}
public int Insert (object obj, string extra, Type objType)
{
    if (obj == null || objType == null) {
        return 0;
    }


    var map = GetMapping (objType);

    #if NETFX_CORE
    if (map.PK != null && map.PK.IsAutoGuid)
    {
        // no GetProperty so search our way up the inheritance chain till we find it
        PropertyInfo prop;
        while (objType != null)
        {
            var info = objType.GetTypeInfo();
            prop = info.GetDeclaredProperty(map.PK.PropertyName);
            if (prop != null) 
            {
                if (prop.GetValue(obj, null).Equals(Guid.Empty))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                break; 
            }

            objType = info.BaseType;
        }
    }
    #else
    if (map.PK != null && map.PK.IsAutoGuid) {
        var prop = objType.GetProperty(map.PK.PropertyName);
        if (prop != null) {
            if (prop.GetValue(obj, null).Equals(Guid.Empty)) {
                prop.SetValue(obj, Guid.NewGuid(), null);
            }
        }
    }
    #endif


    var replacing = string.Compare (extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0;

    var cols = replacing ? map.InsertOrReplaceColumns : map.InsertColumns;
    var vals = new object[cols.Length];
    for (var i = 0; i < vals.Length; i++) {
        vals [i] = cols [i].GetValue (obj);
    }

    var insertCmd = map.GetInsertCommand (this, extra);
    var count = insertCmd.ExecuteNonQuery (vals);
    long id = 0;    //New line
    if (map.HasAutoIncPK)
    {
        id = SQLite3.LastInsertRowid (Handle);  //Updated line
        map.SetAutoIncPK (obj, id);
    }

    //Updated lines
    //return count; //count is row affected, id is primary key
    return (int)id;
    //Updated lines
}