2014-05-09 2 views
0

TL에서 단일 행, 박사 : 나는 ONE DataTable의 행을 업데이트 한 다음이 전체의 DataTable (안 DataTable을 그냥 행을) 다시 캐시 할 그래서 나는업데이트 DataTable을

내가 '나중에 사용할 수 있습니다 m 캐시를 사용하여 MSSQL 데이터베이스를 기반으로 SqlAdapter로 채운 대형 DataTable을 저장하십시오.

이 캐시를 사용하여 DataTable을 얻은 다음이를 사용하여 웹 페이지의 테이블을 표시합니다.

그러나이 DataTable에는 사용자 목록이 포함되어 있으며 이러한 사용자를 쉽게 편집 할 수 있습니다 (MSSQL 데이터베이스에서 편집 할 수 있음).

문제는 각 SQL 업데이트 후에 DataTable을 다시 캐싱해야한다는 것입니다 (그렇지 않으면 데이터베이스에서만 업데이트되지만 DataTable/웹 페이지에서는 업데이트되지 않습니다). 그리고 매우 커서 그것은 매우 짜증나게 만듭니다. 아주 간단한 사용자 업데이트는 SQL SELECT를 통해 모든 게시물을 가져온 다음 다시 캐시해야하므로 매우 오랜 시간이 걸릴 것입니다.

이렇게하면 DataTable에서 특정 행을 바로 업데이트하려고합니다. 내 SQL 업데이트, 이렇게하면 전체 SQL 테이블을 다시 가져올 필요가 없습니다. (너무 커서 SQL SELECT 부분은 너무 오래 걸립니다.)

지금까지이 작업을 수행했습니다.

//We just updated our user, now we'll fetch that with SQL and put it in a new fresh DataTable(that contains just that one row) - since this is much faster than getting the whole table 
//Then we'll use that DataTable containing one fresh row to update our old DataTable and re-cache it 
DataTable newDataTable = getUserByID(userID); //Get our just edited DataTable row 
DataTable cachedDataTable = getSetUserCache(); //Get our cached DataTable 

DataRow oldRow = cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault(); //Get the old row that contains the correct ID 

string test = oldRow["status"].ToString(); //Contains the old and cached value before it got edited 

oldRow = newDataTable.Rows[0]; //Update the old row with the new row 

string test2 = oldRow["status"].ToString(); //Now it contains the new edited value 

//Here I should update the cachedDataTable with the new row updated row 

DataRow oldRowAfterUpdated = cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault(); //Get the old row that now should be updated but isn't 

string test3 = oldRowAfterUpdated["status"].ToString(); //Still contains the old and cached value before it got edited 

success = updateUserCache(cachedDataTable); //Update the DataTable cache that we'll be using later on 

행을 업데이트하는 방법에 대한 게시물 만 볼 수는 있지만 어떻게 DataTable 자체를 새 행으로 실제로 업데이트합니까?

솔루션 :

cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault().ItemArray = newDataTable.Rows[0].ItemArray; 
+0

을 ?? –

+0

@ X-Developer 이미 볼 수있는 것처럼 전체 행 코드에서 수행합니까 – HenrikP

+0

열이 동일하면 모든 열 데이터를 업데이트 할 수 없습니까 ??? –

답변

1

난 당신의 DataRow의 ItemArray 재산 사용할 수 있다고 생각 : 유 전체 행 또는 특정 열을 업데이트 할

void Main() 
{ 
    DataTable tableOld = new DataTable(); 
    tableOld.Columns.Add("ID", typeof(int)); 
    tableOld.Columns.Add("Name", typeof(string)); 

    tableOld.Rows.Add(1, "1"); 
    tableOld.Rows.Add(2, "2"); 
    tableOld.Rows.Add(3, "3"); 

    DataTable tableNew = new DataTable(); 
    tableNew.Columns.Add("ID", typeof(int)); 
    tableNew.Columns.Add("Name", typeof(string)); 

    tableNew.Rows.Add(1, "1"); 
    tableNew.Rows.Add(2, "2"); 
    tableNew.Rows.Add(3, "33"); 

    tableOld.Rows[2].ItemArray = tableNew.Rows[2].ItemArray; //update specific row of tableOld with new values 

    //tableOld.Dump(); 
} 
+0

예, 마지막 부분은 모두 필요한 것입니다. 감사! – HenrikP