多个SQL查询asp.net c#

我需要在一个函数中运行几个查询,我是否必须为每个函数创建一个新的SqlConnection? 或者有一个连接,但不同的SqlCommands也可以工作?

谢谢,

编辑:这会有用吗?

using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query1, conn)) { cmd.ExecuteNonQuery(); } using (SqlCommand cmd = new SqlCommand(query2, conn)) { cmd.ExecuteNonQuery(); } using (SqlCommand cmd = new SqlCommand(query3, conn)) { cmd.ExecuteNonQuery(); } } 

使用MDSN文档作为基础:

 using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql1 = "SELECT ID,FirstName,LastName FROM VP_PERSON"; string sql2 = "SELECT Address,City,State,Code FROM VP_ADDRESS"; using (SqlCommand command = new SqlCommand(sql1,connection)) { //Command 1 using (SqlDataReader reader = command.ExecuteReader()) { // reader.Read iteration etc } } // command is disposed. using (SqlCommand command = new SqlCommand(sql2,connection)) { //Command 1 using (SqlDataReader reader = command.ExecuteReader()) { // reader.Read iteration etc } } // command is disposed. // If you don't using using on your SqlCommands you need to dispose of them // by calling command.Dispose(); on the command after you're done. } // the SqlConnection will be disposed 

你走哪条路并不重要。

SqlConnections由操作系统池化。 您可以连续数千次打开和关闭连接,而不会产生任何性能或其他惩罚。

它是如何工作的:

  1. 应用程序发出创建数据库连接的请求( var c = new SqlConnection(...)
  2. 操作系统连接池查看是否有空闲连接。 如果是这样,你会得到一个参考。 如果没有,那么它会旋转一个新的。
  3. 应用程序表明已完成连接( c.Dispose()
  4. 操作系统会保持连接打开一段时间,以防您的应用程序或其他应用程序尝试创建与该同一资源的另一个连接。
  5. 如果该连接保持空闲直到超时时间过去,则操作系统最终关闭并释放。

这就是为什么一次连接到数据库时,可能需要一秒钟才能处理命令。 但是,如果您关闭它并重新打开它,那么连接立即可用。 更多信息请访问: http : //msdn.microsoft.com/en-us/library/8xx3tyca(v = vs.110).aspx

现在,对于您的代码,一般来说,每次进行SqlCommand调用时都会打开1个SqlConnection; 但是,在SqlConnection using子句下的同一个块内进行多个SqlCommand调用是完全可以接受/合理的。

请记住,您不希望在代码中保留SqlConnection对象超过绝对必要的时间。 这可能会导致很多潜在问题,尤其是在进行Web开发时。 这意味着你的代码快速连续打开和关闭100个SqlConnection对象远比保持该对象并通过各种方法传递它更好。

只打开一个SQLConnection

使用keyworkd 使用它将自动处理连接。

如果为每个打开连接,则可能会出现性能问题。

例:

 using (SqlConnection con = new SqlConnection(connectionString)) { // // Open the SqlConnection. // con.Open(); // // The following code shows how you can use an SqlCommand based on the SqlConnection. // using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2)); } } } 

还有一个例子:

 public DataTable GetData() { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection("your connection here") { con.Open(); using (SqlCommand cmd = con.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "your stored procedure here"; using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } } } return dt; } 

拥有一个SqlConnection和许多SqlCommands都可以正常工作,但是在尝试运行其他命令之前,必须确保丢弃从先前命令返回的任何SqlDataReaders

 using (SqlConnection conn = new SqlConnection()) { conn.Open() using (SqlCommand cmd = new SqlCommand("SELECT myrow FROM mytable", conn)) { using (SqlDataReader reader = cmd.ExecuteReader()) { // Handle first resultset here } } using (SqlCommand cmd = new SqlCommand("SELECT otherrow FROM othertable", conn)) { using (SqlDataReader reader = cmd.ExecuteReader()) { // Handle second resultset here } } } 

或者,您可以将命令组合成一个批处理,然后处理多个结果集,如下所示:

 using (SqlConnection conn = new SqlConnection()) { conn.Open() using (SqlCommand cmd = new SqlCommand("SELECT myrow FROM mytable; SELECT otherrow FROM othertable", conn)) { using (SqlDataReader reader = cmd.ExecuteReader()) { // Handle first resultset here, and then when done call if (reader.NextResult()) { // Handle second resultset here } } } } 

当您处理许多结果集时,您会发现将这样的查询批处理可以显着提高性能,但是它会以您调用代码中增加的复杂性为代价。

纯粹作为using语句的替代:

 SqlConnection con = new SqlConnection(myConnectionString); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = @"SELECT [stuff] FROM [tableOfStuff]"; con.Open(); SqlDataReader dr = null; try { dr = cmd.ExecuteReader(); while(dr.Read()) { // Populate your business objects/data tables/whatever } } catch(SomeTypeOfException ex){ /* handle exception */ } // Manually call Dispose()... if(con != null) con.Dispose(); if(cmd != null) cmd.Dispose(); if(dr != null) dr.Dispose(); 

this和using语句之间的主要区别是,这将允许您更干净地处理exception。