如何简化C#查询执行?

时间:2012-12-18 06:03:00

标签: c# asp.net

我是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();

3 个答案:

答案 0 :(得分: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());
        }

答案 1 :(得分:-1)

    private void button1_Click(object sender, EventArgs e)
    {
        con.Open();   
        cmd.Connection = con;
        cmd.CommandText = "select empid from table1";
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            listBox1.Items.Add(reader[0].ToString());
        }
        reader.Close();
        cmd.CommandText = "select empname from table1";
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            listBox2.Items.Add(reader[0].ToString());
        }
        reader.Close();
        cmd.CommandText = "select empsalary from table1";
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            listBox3.Items.Add(reader[0].ToString());
        }
        reader.Close();
        con.Close();
    }        

答案 2 :(得分:-2)

也许use可以使用EnterpriseLibrary,减少处理数据库的代码量。