2011-07-26 38 views
0

Ado.net不在Windows手機中,因此在插入操作後如何獲得主鍵?如何在插入SQL Ce後獲得主鍵對於windows phone

在SQL方法:

 
SELECT @@IDENTITY 

因此,對於SQL CE在Windows手機,如何使用LINQ做它的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 
+0

克勞斯:哇。它簡短而簡單,但我不確定是否習慣了舊的方式。請參閱上面的我的代碼。 – MilkBottle