0

我是 asp.net 的程序员。我正在使用 C#。我每次都为查询执行编写了很长的代码。如何重构和组织以下代码?

MySqlConnection connection = new MySqlConnection(connstring);
string query = "Select fo_region_Name from fo_region where fo_region_DeleteStatus=0";
MySqlCommand command = new MySqlCommand(query, connection);
MySqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();

query = "Select Fo_Nationality_Name from fo_Nationality a, Fo_region b where a.Fo_Nationality_Type=1 and "
        + "LEFT(a.Fo_Nationality_Code,2)=LEFT(b.fo_region_Name,2)  and  a.Fo_Nationality_DeleteStatus=0 and "
        + "b.fo_region_DeleteStatus=0 Union Select Fo_Nationality_Name from fo_nationality where Fo_Nationality_DeleteStatus=0";
command = new MySqlCommand(query, connection);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();

query = "select mcs_CreditCard_CardName from mcs_creditcard where mcs_CreditCard_DeleteStatus=0";
command = new MySqlCommand(query, connection);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    ddl_CreditCard.Items.Add(UppercaseFirst(reader[0].ToString()));
}
connection.Close();
4

2 回答 2

2

一些想法:

  • 使用多行字符串来格式化您的 SQL 语句。
  • 无需在每个命令执行之间关闭和重新打开连接。
  • 也不需要创建新的连接和命令对象(在这种情况下)
    • 如果您在命令对象上有参数,则创建新命令对象比清除旧参数更容易
  • 使用var语句让 C# 编译器自动为您确定变量类型。
  • 将需要处理的对象包装在一个using块中。

using (var connection = new MySqlConnection(connstring)) {
    connection.Open();

    using (var command = new MySqlCommand()) {
        MySqlDataReader reader;

        command.CommandText = @"
            SELECT fo_region_Name
            FROM fo_region
            WHERE fo_region_DeleteStatus=0
        ";
        using (reader = command.ExecuteReader()) {
            while (reader.Read()) {
                ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
            }
        }

        command.CommandText = @"
            SELECT Fo_Nationality_Name
            FROM fo_Nationality a,
                Fo_region b
            WHERE a.Fo_Nationality_Type = 1
                AND LEFT(a.Fo_Nationality_Code,2) = LEFT(b.fo_region_Name,2)
                AND b.fo_region_DeleteStatus=0

            UNION SELECT Fo_Nationality_Name 
            FROM fo_nationality
            WHERE Fo_Nationality_DeleteStatus=0
        ";
        using (reader = command.ExecuteReader()) {
            while (reader.Read()) {
                ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
            }
        }

        command.CommandText = @"
            SELECT mcs_CreditCard_CardName
            FROM mcs_creditcard
            WHERE mcs_CreditCard_DeleteStatus = 0
        ";
        using (reader = command.ExecuteReader()) {
            while (reader.Read()) {
                ddl_Nationality.Items.Add(UppercaseFirst(reader[0].ToString()));
            }
        }
    }
}

使用 LINQ(添加using System.Data.Common语句):

        using (reader = command.ExecuteReader()) {
            /*while (reader.Read()) {
                ddl_Country.Items.Add(UppercaseFirst(reader[0].ToString()));
            }*/
            ddl_Country.Items.AddRange((
                from DbDataRecord row in reader
                select new ListItem(
                    UppercaseFirst(reader.GetString(0))
                )
            ).ToArray());
        }
于 2012-12-18T07:29:24.927 回答
-2

Maybe use can use EnterpriseLibrary, to reduce amount of code that deals with database.

于 2012-12-18T06:34:11.030 回答