1
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# 中的第一个项目。如何在不打开许多连接的情况下编写一些查询?谁能帮我?

4

5 回答 5

3
  1. 用于using-statement确保即使在异常情况下也能关闭连接。当类实现时,您应该始终使用它IDisposable
  2. Connection-Pooling您调用con.Open()或时,您并不总是打开和关闭连接con.Close()。实际上Close只是使连接可重用,否则它将被标记为“正在使用”。因此,最好尽快关闭连接。

您可以使用 aDataAdapter来填充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;for Linq-To-DataTable

于 2012-10-06T20:36:11.207 回答
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();
        }
于 2012-10-06T20:26:25.563 回答
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 查询来检索标签的性能会更好。

于 2012-10-06T20:27:09.827 回答
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 运算符

于 2012-10-06T20:31:11.110 回答
0

只是展示了另一种方法,它只需要一个连接、一个命令和一个数据读取器。

虽然 Tim Schmelter 方法在您的情况下是最有效的,但这是 aNextResult方法的演示DataReader

注意SqlCommand包含的 3 个子查询中的 sql 查询是如何用分号分隔的。每当您调用NextResult时,您都会转到下一个查询。

using (var connection = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True"))
using (var command = new SqlCommand(
@"select label_sh from label_text where label_form='2' and label_form_labelID='1';
select label_sh from label_text where label_form='2' and label_form_labelID='2';
select label_sh from label_text where label_form='2' and label_form_labelID='3'", connection))
using (var reader = command.ExecuteReader())
{
    var label1 = reader["label_sh"];

    reader.NextResult();

    var label2 = reader["label_sh"];

    reader.NextResult();

    var label3 = reader["label_sh"];
}
于 2012-10-06T20:46:29.730 回答