C# 使用一个sql连接的几个查询
我从数据库中获取标签文本。但在每次抓取操作中,我都会打开一个连接来编写查询。这是我在C语言中的第一个项目。如何在不打开许多连接的情况下编写一些查询?有人能帮我吗?没有必要每次都关闭连接。您甚至可以在示例中重用SqlCommand变量C# 使用一个sql连接的几个查询,c#,C#,我从数据库中获取标签文本。但在每次抓取操作中,我都会打开一个连接来编写查询。这是我在C语言中的第一个项目。如何在不打开许多连接的情况下编写一些查询?有人能帮我吗?没有必要每次都关闭连接。您甚至可以在示例中重用SqlCommand变量 private void button5_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;
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();
}
没有必要每次都关闭连接。您甚至可以在示例中重用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();
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();
}
您可以对所有SqlCommand对象重复使用SqlConnection,完成后可以关闭SqlConnection:
但是,创建一个SQL查询来检索标签对性能更为有利。您可以对所有SqlCommand对象重复使用SqlConnection,完成后,您可以关闭SqlConnection:
但是创建一个SQL查询来检索标签对性能更有利。好吧,我建议您只创建一个到de DB的连接
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 IN运算符只进行一个这样的查询
SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
我建议您只创建一个到de DB的连接
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 IN运算符只进行一个这样的查询
SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
使用using语句确保即使在发生异常的情况下也关闭连接。当类实现IDisposable时,应该始终使用它。
当你呼叫con.Open或con.Close时,你并不总是打开和关闭连接。实际上,Close只是使连接可重用,否则它将被标记为正在使用。因此,最好尽快关闭连接。
您可以使用DataAdapter用一个查询填充DataTable。然后,您将拥有所有三个记录,并可以获取您需要的:
select label_sh
from label_text
where label_form='2' and label_form_labelID IN ('1','2','3')
请注意,您需要使用System.Linq;对于Linq到DataTable
使用using语句确保即使在发生异常的情况下也关闭连接。当类实现IDisposable时,应该始终使用它。
当你呼叫con.Open或con.Close时,你并不总是打开和关闭连接。实际上,Close只是使连接可重用,否则它将被标记为正在使用。因此,最好尽快关闭连接。
您可以使用DataAdapter用一个查询填充DataTable。然后,您将拥有所有三个记录,并可以获取您需要的:
select label_sh
from label_text
where label_form='2' and label_form_labelID IN ('1','2','3')
请注意,您需要使用System.Linq;对于Linq到DataTable 只是显示另一种方法,它只需要一个连接、一个命令和一个数据读取器 虽然Tim Schmelter方法在您的案例中最有效,但这是DataReader的NextResult方法的演示 请注意,SqlCommand中的sql查询如何包含3个子查询,子查询之间用分号分隔。无论何时调用NextResult,都会转到下一个查询
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");
只是展示了另一种方法,它只需要一个连接、一个命令和一个数据读取器 虽然Tim Schmelter方法在您的案例中最有效,但这是DataReader的NextResult方法的演示 请注意,SqlCommand中的sql查询如何包含3个子查询,子查询之间用分号分隔。无论何时调用NextResult,都会转到下一个查询
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");