开发者

c# code error The name 'i' does not exist

in line

 urls[i] =  Reader.GetValue(i).ToString();  

it say Error 1 The name 'i' does not exist in the current context

how can I fix it

private void Form1_Load(object sender, EventArgs e)
    {
        string MyConString = "SERVER=192.168.0.78;" +
             "DATABASE=webboard;" +
             "UID=aimja;" +
             "PASSWORD=aimjawork;" +
             "charset=utf8;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        command.CommandText = "SELECT  url FROM `listweb` WHERE `url` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
        connection.Open();
        Reader = command.ExecuteReader();


        string[] urls = new string[2];
        string thisrow = "";
        string sumthisrow = "开发者_开发技巧";
        string urlname ;
        while (Reader.Read())
        {
            thisrow = "";
            for (int i = 0; i < Reader.FieldCount; i++)
                thisrow +=  Reader.GetValue(i).ToString();
                urlname = Reader.GetValue(i).ToString();

                urls[i] =  Reader.GetValue(i).ToString(); 

          //  System.IO.File.AppendAllText(@"C:\file.txt", thisrow + " " + Environment.NewLine);
            sumthisrow = sumthisrow + thisrow;


You need to add braces to your for loop otherwise it only loops the first statement.

for (int i = 0; i < Reader.FieldCount; i++)
{
    thisrow +=  Reader.GetValue(i).ToString();
    urlname = Reader.GetValue(i).ToString();
    urls[i] =  Reader.GetValue(i).ToString(); 
}


You are missing braces here:

for (int i = 0; i < Reader.FieldCount; i++)
{
    thisrow +=  Reader.GetValue(i).ToString();
    urlname = Reader.GetValue(i).ToString();
    urls[i] = Reader.GetValue(i).ToString(); 
}

I'd also advise you not to create strings by concatenating in a loop. Put them in a List<string> first then at the end convert it to an array (except in .NET 4.0 or newer where this step is not required) and use string.Join. As well as giving better performance this allows allows you to add a separator between the fields, which I assume you want...

If you don't need a separator then you can use a StringBuilder.


Your braces are missing for the FOR loop. The variable i is available only within the FOR loop which is only one line after your loop in your case.

for (int i = 0; i < Reader.FieldCount; i++)
{
    thisrow +=  Reader.GetValue(i).ToString();
    urlname = Reader.GetValue(i).ToString();

    urls[i] =  Reader.GetValue(i).ToString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜