在阅读器中调用变量时出错

时间:2014-02-28 15:46:07

标签: c# arrays

我有这段代码:

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

*这是遇到的另一个问题。请忽略前一个。

2 个答案:

答案 0 :(得分:1)

我怀疑

Convert.ToBoolean(Text) 

评估为假

您是否在调试中单步执行了代码?

为什么读者只需一行:

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

答案 1 :(得分: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语句