Multipleactiveresultsets can make database connections reusable. This will not be afraid of the database connection resources are depleted. The use of the method is simple, just add it to the data connection string.
For example: server= (local), integrated security = True;database=adventureworks; Multipleactiveresultsets=true;
Test Cases:
Using System; Using System.Threading; Using System.Data.SqlClient; Using System.Configuration; Namespace ConsoleApplication1 {public class Example {public static void Main () {SqlConnection sql1 = new SqlConnection ( "Server= (local), integrated security = True;database=adventureworks;"); SQL1. Open (); SqlCommand comm1 = new SqlCommand (); Comm1.commandtext = "Select 1"; Comm1.commandtype = System.Data.CommandType.Text; Comm1. Connection = SQL1; Comm1. ExecuteNonQuery (); SQL1. Close (); Console.ReadLine (); } } }
After compiling, open the Bin/debug/consoleapplication1.exe.
Open a new window in SQL Server 2008 Management Studio, enter sp_who
By F5 execution, you can see that a user is already connected to the AdventureWorks database.
Then open a ConsoleApplication1.exe and find that one more user will be connected to the AdventureWorks database.
Now change the connection string of the program to server= (local), integrated security = True;database=adventureworks; Multipleactiveresultsets=true;
Executed in the order above, it is found that no user or only one user is connected to the AdventureWorks database, regardless of how many ConsoleApplication1.exe are open.
This is the benefit of database connectivity reuse.
For additional uses of database connection strings, see: