G
Guest
Hello, what is the proper way to handle sql-connections in webservices?
1. Creating a SqlConnection object and opening connection in each web
method? Example:
[WebMethod]
public Clients GetClients()
{
using(SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
using(SqlCommand
.......
}
}
2. Or maybe define SqlConnection once as public variable and in every
web method only opening this connection?
Example:
public class Service1 : System.Web.Services.WebService
{
public SqlConnection conn1 = new SqlConnection();
....
....
[WebMethod]
public Clients GetClients()
{
conn.Open();
using(SqlConnection = new ...
...
}
Which is a good example?
1. Creating a SqlConnection object and opening connection in each web
method? Example:
[WebMethod]
public Clients GetClients()
{
using(SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
using(SqlCommand
.......
}
}
2. Or maybe define SqlConnection once as public variable and in every
web method only opening this connection?
Example:
public class Service1 : System.Web.Services.WebService
{
public SqlConnection conn1 = new SqlConnection();
....
....
[WebMethod]
public Clients GetClients()
{
conn.Open();
using(SqlConnection = new ...
...
}
Which is a good example?