2013-02-07 32 views
0

我正在循環通過中繼器和發射更新查詢到我的分貝與C#的SqlCommandLINQ到SQL沒有得到更新DB數據

後來當我想從最近更新的類型,我使用的實體類型

Document d = (from c in db.Documents where c.Id = myID select c).First(); 

我得到一個文檔,其中該更新使用SqlCommand的領域仍然是零,但是當我檢查數據庫,字段的值正確。

有沒有辦法從SqlCommand命令獲取實體類型,或者確保Linq-To-Sql獲取更新的數據。

我已經嘗試在執行此操作之前運行db.savechanges(),但沒有區別。

編輯:

SqlCommand command = getCommand(type, field, item, nDocID, required); // commandText = "UPDATE Documents SET " + field + "[email protected]" + field + " WHERE DocumentID = " + id; " 



        if (command != null) 
        { 
         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString); 

         command.Connection = conn; 
         conn.Open(); 
         command.ExecuteNonQuery(); 
         conn.Close(); 
        } 
+0

你能告訴我們代碼,你在哪裏更新它。 – Tabish

+0

添加了我執行sqlcommand – KristianMedK

+0

的行,您可以像其他人所建議的那樣刷新數據上下文,但根據我的經驗,這確實會影響性能。我認爲你最好通過Linq2Sql來完成所有的數據庫交互,而不是像現在那樣直接使用sql命令進行混合。 – RobJohnson

回答

3

db.SaveChanges()不會做任何事,因爲你並沒有通過EF任何變更申請。您所做的更改不在其範圍之內。我猜你聲明你的數據庫變量全局和EF緩存數據。相反,嘗試這樣做:

using(somethingEntities db = new somethingEntities()){ 
    Document d = (from c in db.Documents where c.Id = myID select c).First(); 
    // do something here 
}