-2

我想在表单加载中获取我的数据库中的多个数据。但我发现代码很长而且重复。有人可以让这段代码更短一点吗?

这是我的代码

private void Form1_Load(object sender, EventArgs e)
    {
        string a = label1.Text;
        string connString = "Server=Localhost;Database=this;Uid=root;password=root";
        using (var connection = new MySqlConnection(connString))
        {
            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)");
                command.Parameters.AddWithValue("?room", a);
                command.ExecuteNonQuery();

                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    button1.Text = reader["Room_name"].ToString();
                }
            }
        }
        string b = label2.Text;
        string connString2 = "Server=Localhost;Database=this;Uid=root;password=root";
        using (var connection = new MySqlConnection(connString2))
        {
            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)");
                command.Parameters.AddWithValue("?room", b);
                command.ExecuteNonQuery();

                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    button2.Text = reader["Room_name"].ToString();
                }
            }

        }
    }
4

2 回答 2

1

首先,您要连接到同一个数据库两次。而且您正在以相同的方法进行操作(很明显,您完全有权决定是否只连接一次)。

其次,您可以将实际的选择执行包装在一个单独的方法中。该单独的方法应该从调用者那里接收它的连接(以便在这种特殊情况下您只能将 ONCE 连接到数据库,但可以根据需要进行尽可能多的选择)。

第三,不要使用ExecuteReader,使用ExecuteScalar。

此外:您不应该在 FormLoad 上做太多事情(UI 将冻结)。为什么要根据标签的值查询数据库?那只是您快速编写的示例以向我们展示您的意思还是实际的业务逻辑?

private string GetRoomName_BasedOn_RoomNumber(string roomNumber, MyConnection connection) {
    using (var command = connection.CreateCommand())
    {
         command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)");
         command.Parameters.AddWithValue("?room", roomNumber);
         //command.ExecuteNonQuery();

         object response = command.ExecuteScalar();

         return response as string; // consider <null> as a "No such Room Number" signal
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    string a = label1.Text;
    string b = label2.Text;

    string connString = "Server=Localhost;Database=this;Uid=root;password=root";
    using (var connection = new MySqlConnection(connString))
    {
       connection.Open();
       button1.Text = this.GetRoomName_BasedOn_RoomNumber(a, connection);
       button2.Text = this.GetRoomName_BasedOn_RoomNumber(b, connection);
    }
}
于 2013-02-25T15:38:15.333 回答
0

例如,您可以重构代码并将以下代码提取到方法中:

  private void DoSomething(MySqlConnection connection)
{            
using (var command = connection.CreateCommand())
        {
            command.CommandText = ("Select Room_name from firstfloor where Room_no=(?room)");
            command.Parameters.AddWithValue("?room", b);
            command.ExecuteNonQuery();

            MySqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                button2.Text = reader["Room_name"].ToString();
            }
        }

    }
于 2013-02-25T15:33:37.820 回答