SQL命令datareader while循环不起作用

时间:2015-08-12 07:46:45

标签: c# sql winforms while-loop

我对SQL命令有疑问。我似乎无法在" while(dr.read())"下运行while循环。下面是我在C#Windows窗体中的示例代码。 谢谢。

                cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT * FROM network";
                MySqlDataReader dr;
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    string datasource = dr[1].ToString();
                    string datadestination = dr[2].ToString();
                    if (source == datasource && destination == datadestination)
                    {
                        int newcounter;
                        newcounter = Convert.ToInt32(dr[4]) + 1;
                        cmd.CommandText = "UPDATE network set counter = @nnnewcounter";
                        cmd.Parameters.AddWithValue("@nnnewcounter", newcounter);
                    }
                    else
                    {
                        cmd.CommandText = "INSERT INTO network(source,destination,length,counter) VALUES (@sssource,@dddestination,@lllength,@cccounter)";
                        cmd.Parameters.AddWithValue("@sssource", source);
                        cmd.Parameters.AddWithValue("@dddestination", destination);
                        cmd.Parameters.AddWithValue("@lllength", length);
                        cmd.Parameters.AddWithValue("@cccounter", 1);
                    }
                }

1 个答案:

答案 0 :(得分:0)

你有一些问题首先你重复使用相同的命令并使用不同的参数,你应该清除它们。您是否正在打开datareader的连接也不清楚。因此,我会在SQLDataReader之外移动插入和更新,因为它会使代码更容易阅读。

using (SqlConnection connection = new SqlConnection(ConnString))
{
    using (SqlCommand cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM network";
        cmd.Connection.Open();

        using (MySqlDataReader dr = cmd.ExecuteReader())
        {
             if (dr .HasRows)
             {
                  while (dr.Read())
                  {
                    string datasource = dr[1].ToString();
                    string datadestination = dr[2].ToString();
                    if (source == datasource && destination == datadestination)
                    {
                        int newcounter;
                        newcounter = Convert.ToInt32(dr[4]) + 1;
                        Updateddos_network(newcounter);
                    }
                    else
                    {
                        Savedoss_network(source,destination, length, 1);
                    }
            }
            else
            {
               //No rows found
            }
        }
    }
 }

然后在你可以拥有的同一课程的方法之外。

            private void Updateddos_network(int newcounter)
            {
                using (SqlConnection connection = new SqlConnection(ConnString))
                {
                   using (SqlCommand cmd = connection.CreateCommand())
                   {
                     cmd.CommandText = "UPDATE ddos_network set counter = @nnnewcounter";
                     cmd.Parameters.AddWithValue("@nnnewcounter", newcounter);
                     cmd.Connection.Open();
                     cmd.ExecuteNonQuery();
                  }
                }
            }


            private void Insertddos_network(string source, string destination, int length, int counter)
            {
                using (SqlConnection connection = new SqlConnection(ConnString))
                {
                   using (SqlCommand cmd = connection.CreateCommand())
                   {
                     cmd.CommandText = "INSERT INTO ddos_network(source,destination,length,counter) VALUES (@sssource,@dddestination,@lllength,@cccounter)";
                     cmd.Parameters.AddWithValue("@sssource", source);
                     cmd.Parameters.AddWithValue("@dddestination", destination);
                     cmd.Parameters.AddWithValue("@lllength", length);
                     cmd.Parameters.AddWithValue("@cccounter", counter);
                     cmd.Connection.Open();
                     cmd.ExecuteNonQuery();
                  }
                }
            }

如果你要进一步重构你的代码,你可以为你的ddos_network对象设置一个Save方法,然后根据当前对象是否具有Id来更新或插入对象,然后用它来进行除法。