简体   繁体   中英

What is the best practice in ASP.NET for re-using a database connection for MS Access migrating to SQL server?

What is the best practice for opening and re-using a database connection in an ASP.NET web service (C# 4.0) that has about 5 web methods that use the same database? The answer ideally will be the same for MS Access and SQL server.

  1. Global private variable:

    a. Advantage: Easy to maintain if change database providers

    b. Disadvantage: Cannot use the 'using' clause and connection may stay open too long – how long will connection stay open approximately?

  2. Pass the db connection as a method parameter:

    a. Advantage: can close connection when not needed use 'using' clause

    b. Disadvantage: annoying to maintain and pass param everywhere

  3. Open and close the connection with using clause on demand when needed in any method:

    a. Question: Is this the slowest because you have to re-open the same connection multiple times? OR because of behind-the-scenes connection pooling this is not slower? Thus this would probably be the best practice…?

    b. Question: Is connection pooling only relevant for SQL server, thus would not work for MS Access?

At first I was thinking of using a global private variable (OleDbConnection) as I use the connection to the main database a fair bit in the web methods and supporting methods and will be migrating from MS Access to SQL server in the near future and will be easy to make the change.

But after reading up on some articles like Obtaining SQL connection most efficiently when using ASP.NET and web services

it seems like I can just create a new connection on demand anywhere with the 'using' clause and not suffer a performance hit?

ASP.Net pools database connections so you tend not to have to worry about these low-level details. Take a look at Tip 3 of this MSDN article .

Best practice is to not re-use your Connection objects at all.

public void GetEmployees() As List<Employee> {
    var employees = new List<Employee>();
    using (var connection = new SqlConnection(Configuration.ConnectionString)) {
        using (var command = connection.CreateCommand()) {
            command.CommandText = "SELECT * FROM dbo.Employee";
            connection.Open();
            using (var reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    employees.Add(Employee.CreateRecordFromOpenReader(reader));
                }
            }
        }
    }

    return employees;
}

Then if you need transactions from whatever uses this, setup DTC and do this.

using (var scope = new TransactionScope()) {
    var employees = GetEmployees();
    employees.Map((e) => e.Status = Status.Active);
    scope.Complete();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM