一个sql连接的几个查询

时间:2012-10-06 20:19:55

标签: c#

private void button5_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='1'", conn);
    conn.Open();
    label1.Text = cmd.ExecuteReader().ToString();
    conn.Close();

    SqlConnection conn1 = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
    SqlCommand cmd1 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='2'", conn1);
    conn1.Open();
    label2.Text = cmd1.ExecuteReader().ToString();
    conn1.Close();

    SqlConnection conn2 = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
    SqlCommand cmd2 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='3'", conn2);
    conn2.Open();
    label3.Text = cmd2.ExecuteReader().ToString();
    conn2.Close();
}

我从数据库中获取标签文本。但是在每次提取操作中,我都会打开一个连接来编写查询。这是我在C#中的第一个项目。如何在不打开多个连接的情况下编写一些查询?有谁可以帮助我?

5 个答案:

答案 0 :(得分:3)

  1. 使用using-statement确保连接即使在异常的情况下也会关闭。当类实现IDisposable时,您应该始终使用它。
  2. 使用Connection-Pooling,当您致电con.Open()con.Close()时,您并不总是打开和关闭连接。实际上Close只是使连接可重用,否则它将被标记为“正在使用中”。因此,尽快关闭连接是一种很好的做法。
  3. 您可以使用DataAdapter通过一个查询填充DataTable。那么你将拥有所有三个记录并可以获得你需要的东西:

    using (var conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True"))
    {
        var sql = "select label_sh from label_text where label_form_labelID IN('1','2','3') and label_form='2'";
        using (var da = new SqlDataAdapter(sql, conn))
        {
            da.Fill(table); // you don't need to open a connection when using a DataAdapter
        }
    }
    
    label1.Text = table.AsEnumerable()
                       .Single(r => r.Field<int>("label_form_labelID") == 1)
                       .Field<String>("label_sh");
    label2.Text = table.AsEnumerable()
                       .Single(r => r.Field<int>("label_form_labelID") == 2)
                       .Field<String>("label_sh");
    label3.Text = table.AsEnumerable()
                      .Single(r => r.Field<int>("label_form_labelID") == 3)
                      .Field<String>("label_sh");
    

    请注意,您需要为using System.Linq;添加Linq-To-DataTable

答案 1 :(得分:2)

每次都不需要关闭连接。您甚至可以在示例中重用SqlCommand变量。

private void button5_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");

            SqlCommand cmd = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='1'", conn);
            conn.Open();
            label1.Text = cmd.ExecuteReader().ToString();

            cmd.CommandText ="select label_sh from label_text where label_form='2' and label_form_labelID='2'";
            label2.Text = cmd.ExecuteReader().ToString();

            cmd.CommandText = "select label_sh from label_text where label_form='2' and label_form_labelID='3'"
            label3.Text = cmd.ExecuteReader().ToString();

            conn.Close();
        }

答案 2 :(得分:1)

您可以将SqlConnection重新用于所有SqlCommand个对象,完成后可以关闭SqlConnection

SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
conn.Open();

SqlCommand cmd = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='1'", conn);    
label1.Text = cmd.ExecuteReader().ToString();

SqlCommand cmd1 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='2'", conn);    
label2.Text = cmd1.ExecuteReader().ToString();  


SqlCommand cmd2 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='3'", conn);   
label3.Text = cmd2.ExecuteReader().ToString();

conn.Close();

但是,创建一个SQL查询以检索标签的性能甚至更好。

答案 3 :(得分:1)

好吧,我建议您只创建一个与de DB的连接

 SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");

然后您可以使用SQL IN运算符只进行一次这样的查询

select label_sh 
from label_text 
where label_form='2' and label_form_labelID IN ('1','2','3')

SQL IN Operator

答案 4 :(得分:0)

只显示另一种方法,只需要一个连接,一个命令和一个数据读取器。

虽然Tim Schmelter方法在您的案例中是最有效的,但这是NextResult的{​​{1}}方法的演示。

注意DataReader中的sql查询如何包含以分号分隔的3个子查询。每当您致电SqlCommand时,您都会转到下一个查询。

NextResult