Ado.net不在Windows手机中,那么如何在插入操作后获取主键?
SQL中有方法:
SELECT @@IDENTITY
因此,对于Windows手机中的SQl Ce,如何使用Linq To SQL或其他方法。
由于
---更新:
这是您在列中引用的onInsert吗?
[Column(Storage="_CtcId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int CtcId
{
get
{
return this._CtcId;
}
set
{
if ((this._CtcId != value))
{
this.OnCtcIdChanging(value);
this.SendPropertyChanging();
this._CtcId = value;
this.SendPropertyChanged("CtcId");
this.OnCtcIdChanged();
}
}
}
我收到了返回ID。请告知这是否是在插入操作后获取主键的正确方法,如下所示:
using (DBContacts context = new DBContacts(ConnectionString)) { //--- create object first: TblContacts tblCtc = new TblContacts(); tblCtc.FirstName = txtFirstName.Text.Trim(); tblCtc.LastName =txtLastName.Text.Trim(); tblCtc.Birthday = txtBirthday.Text.Trim(); tblCtc.NickName = txtNickName.Text.Trim(); context.TblContacts.InsertOnSubmit(tblCtc); context.SubmitChanges(); var id = tblCtc.CtcId; MessageBox.Show("Inserted Ok. Id is no :" + id.ToString()); }
答案 0 :(得分:0)
如果您的对象PrimaryKey属性将AutoSync设置为OnInsert,则在插入对象后,该对象将自动更新其密钥。
示例:
var newPerson = new Person() { Name = "Windcape" };
people.Add(newPerson);
var id = newPerson.Id; // Id is now the inserted id