2012-09-06 34 views
3

我寫了一些C#代碼來存儲數據到可改寫的數據庫。但是我不知道如何更新數據。 下面是我做了什麼:
如何更新可改變的數據

Hypertable b = new Hypertable(); 
b.Write("1", "value1");//this line is for writing to database 
b.Write("1", "value111");//this line is for updating the value whose key = "1" 

和我寫的功能是:

public override bool Write(string key, string value) 
{ 
    try 
    { 
     using (IContext context = Context.Create(connectionString)) 
     using (IClient client = context.CreateClient()) 
     { 
      using (INamespace ns = client.OpenNamespace("Test", OpenDispositions.OpenAlways | OpenDispositions.CreateIntermediate)) 
      { 
       using (ITable table = ns.OpenTable(tableName)) 
       { 
        using (ITableMutator mutator = table.CreateMutator()) 
        { 
         Key _key = new Key { Row = key, ColumnFamily = "vvalue" }; 
         mutator.Set(_key, Encoding.UTF8.GetBytes(value)); 
        } 
       } 
      } 
     } 
     return true; 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Hypertable--Write--{0}", e.Message); 
     return false; 
    } 
} 


所以我的更新只需再次使用相同的密鑰,但不同的值寫入。然而,在更新之後,我會讀取該鍵和值,它應該顯示新的鍵和值(鍵=「1」和值=「值111」),但不是,它只顯示舊鍵和值(key =「1」和value =「value1」)
我不知道爲什麼會發生這種情況。任何提示將不勝感激。謝謝。

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

你解決了這個問題嗎?您正在使用哪種版本的hypertable?因爲沒有方法:mutator_set,你必須使用set_cell或set_cell_as_array –

回答

0

你可以嘗試添加mutator.Flush()到你的內心using塊,雖然當突變超出範圍時,應當自動調用...

+0

我的帖子在這裏將有望深入地回答你的問題。 http://stackoverflow.com/questions/17955954/hypertable-loading-data-using-mutators-vs-load-data-infile。我已經嘗試使用mutator加載,並使用平面文件直接加載。祝你好運。 – NullException

相關問題