简体   繁体   中英

Access and close the database automatically in ASP.net C# web form web application

I am developing a web application using MySql database. This is a web form application using .NET Framework 4.5.

Lately I have been developing many functions that are accessing the database in high frequency.

Instead of creating and closing the database connection every time I would like to optimize the opening and closing of the database connections, what is the best approach?

You should research the using keyword. It is a short-hand widely used in the C# programming language.

The following two blocks are synonymous:

var con = new SqlConnection(connectionString);
try
{
     con.Open();
} 
finally
{
     con.Dispose();
}

using (var con = new SqlConnection(connectionString)
{
      con.Open();
}

如果这些函数都在同一类中,则可以创建连接属性(get和set方法)以进行优化

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