Main Page | Report this Page
.NET DotNet Forum Index  »  ASP.NET - Webservices Forum  »  Best way to handle sql-connections in webservices?...
Page 1 of 1    

Best way to handle sql-connections in webservices?...

Author Message
spammtrapp...
Posted: Wed Oct 21, 2009 3:16 am
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?
 
Mr. Arnold...
Posted: Wed Oct 21, 2009 5:59 pm
Guest
"spammtrapp" <gdziestam at (no spam) w.necie> wrote in message
news:hbmjgt$921$1 at (no spam) news.onet.pl...
Quote:
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
 
CSharpner...
Posted: Wed Nov 11, 2009 9:50 am
Guest
I second Mr Arnold's advice.

On Oct 21, 6:59 pm, "Mr. Arnold" <MR. Arn... at (no spam) Arnold.com> wrote:
Quote:
"spammtrapp" <gdzies... at (no spam) w.necie> wrote in message

news:hbmjgt$921$1 at (no spam) news.onet.pl...



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
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Tue Nov 24, 2009 12:44 pm