.net core web api close database connection

asp.net-core-webapi c# entity-framework-6

Question

I am trying to build a blog webapi with this web api core demo and microsoft doc. I use Npgsql connect to a PostgreSQL database, and add some methods, but the database return error message "too many clients connected" when I debugging.

So how to close the connections in webapi? I read some documents but can't find a way to do with it.

my repo here

1
1
11/28/2018 5:19:46 AM

Accepted Answer

The easiest solution is to use using block. This will take care of database connection closing automatically and you don't have to manually close the database connection.

eg.

using (SqlConnection connection = new SqlConnection(connectionString))
{
   // Your database code
}
1
11/29/2018 4:31:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow