Best way to handle sql-connections in webservices?

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?
 
M

Mr. Arnold

spammtrapp said:
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?

Neither one of them are good examples, as a DAL (Data Access Layer) method
should be responsible for its data access of opening the connection,
accessing the data, closing the connection and returning the data to the Web
method and not the Web method doing it.

[WebMethod]
public Clients GetClients()
{
DAL dal = new DAL();

return dal.GetClients();
}


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4530 (20091021) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
C

CSharpner

I second Mr Arnold's advice.

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?

Neither one of them are  good examples, as a DAL (Data Access Layer) method
should be responsible for its data access of opening the connection,
accessing the data, closing the connection and returning the data to the Web
method and not the Web method doing it.

 [WebMethod]
 public Clients GetClients()
 {
       DAL dal = new DAL();

       return dal.GetClients();
 }

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4530 (20091021) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top