2014-02-28 61 views
-1

我有這樣一段代碼:錯誤而在讀者致電變量

while (reader.Read()) 
      { 
       count++; 
       string Text = (String.Format("{0}", Object.Equals(Variables.buffering, reader.GetValue(0)))); 
       List<string> mystring = new List<string>(); 
       mystring.Add(Text); 
       if (Convert.ToBoolean(Text)) 
       { 
        string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart, mainWindow.richtextbox2.Document.ContentEnd).Text; 


        var str = Regex.Replace(myText, @"(|\r?\n)\1+", "$1", RegexOptions.Multiline); 
        mainWindow.Dispatcher.Invoke(new Action(() => 
        mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run("hello"))))); 

        string text = "s"; 
        mainWindow.WriteSerial(text); 
        Console.WriteLine(Text); 
       } 
      } 
      foreach (string element in mystring) 
      { 
       Console.WriteLine(element); 
      } 

這是爲什麼我在foreach循環的MyString中有一個錯誤:

Error 2 The name 'mystring' does not exist in the current context 

*這是另一種遇到的問題。請忽略前一個。

+0

請添加您收到錯誤消息。 –

+0

沒有錯誤。但它並沒有在那裏顯示「你好」字符串 – iaskyou

+0

我不確定在你獲得RowCount後你要做什麼。驗證你的RowCount變量是否包含正確的行數,你應該很好。你爲什麼要調用reader.GetValue(0)?此外,您當然不應該試圖從您的數據訪問代碼寫入您的窗口。 –

回答

0

如果你只是想行數,你沒有選擇在特定的列。只是這樣做:

using(SQLiteConnection sqliteCon = new SQLiteConnection(dBConnectionString)) 
{ 
    sqliteCon.Open(); 
    using(SQLiteCommand createCommand = new SQLiteCommand("SELECT COUNT(*) FROM EmployeeList", sqlliteCon)) 
    { 
     createCommand.CommandType = System.Data.CommandType.Text; 
     int RowCount = 0; 
     RowCount = Convert.ToInt32(createCommand.ExecuteScalar()); 

    } 
} 

更新了正確使用using聲明

+0

CommandType.Text;會產生一個錯誤。但如果我刪除它,那麼讀者將會出現故障。 – iaskyou

+0

嘗試改變CommandType.Text到System.Data.CommandType.Text –

+0

讀者仍然會出現故障。它不會再讀。我將在閱讀sqlite內容時更新我的​​問題以獲取其餘代碼 – iaskyou

1

我懷疑

Convert.ToBoolean(Text) 

計算結果爲假

你通過調試代碼步驟?

爲什麼一個單行讀者:

RowCount = (Int32)createCommand.ExectueScalar.ToString(); 
+0

是的。我沒有測試RowCount和外設。它將返回true – iaskyou

+0

爲什麼要刪除RowCount(如果它是代碼的一部分)?在調試中,你看到了一行mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run(「hello」)))));執行?你說沒有錯誤,但沒有顯示你好?你確定代碼被調用?你看到Console.WriteLine(文本);? – Paparazzi

+0

等。我會嘗試,如果我的代碼真的在while循環 – iaskyou