Solved

ADO.NET SqlConnection

Posted on 2013-06-02
8
244 Views
Last Modified: 2013-06-12
Hello Experts,

I have a question about the following code below. I have a project that I'm creating and the project requires that I need to perform multile Create, Retrieve, Update, and Delete (CRUD) commands that I need to create. I seem to be using the following code below a lot. I was wondering if there was a way to create a class that has the following code below. The only think though that i would need to do if it was possible to create a class with the code below was how would I be able to add parameters and names of my stored procedures to this class since i have to create so many.

    protected void RetrieveDataName()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "NameOfStoredProcedure";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        try
        {
            conn.Open();

            //handle binding logic here
        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            conn.Close();
        }
    }

Open in new window

0
Comment
Question by:asp_net2
  • 4
  • 3
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 39215121
" how would I be able to add parameters and names of my stored procedures to this class since I have to create so many"

Those would be properties of your class.

The could be a Connection Class.

AW
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39215202
Hi Aurthur_Wood,

Sorry, but I'm a little new to making a class and not sure what you can or cannot do with them. I usually just code ADO.NET in the CodeBehind and just now thinking it would be easier to manage if there was a way that I could just make an SqlConnection Class and just manage/update that class for all the Events.

Not sure what you mean by "Those would be properties of your class" and "The could be a Connection Class". Could you explain to me a little better since I'm still learning.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 39215286
Like this (air code)

public class MyConnection
      sqlConnection _conn;
      string _spName;
      sqlCommand _cmd;
      public sqlConnection connection
         {
              get { return _conn};
              set{_conn = value};
          }
       
     public string spName
       {
            get { return _spName;}
            set { _spName = value;}
       }
     
     public MyConnection(sqlConnection conn, string spName)
     {
        _conn  = conn;
        _spName = spName;
        _conn.Open();
        _cmd = new sqlCommand();
        _cmd.Connection = _conn;
        _cmd.CommandText = _spName;
}
       
       
AW
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 4

Author Comment

by:asp_net2
ID: 39217807
Ok, I somewhat understand that. Now how would that be called with the ADO.NET code that I initially supplied above in the RetrieveDataName() Event?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 39218907
RetrieveDataName is not an Event.  It is a method that does not return a value (which makes it almost useless, in your example)


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString);

MyConnection myConn = new MyConnecton(conn, spName);
sqlDataReader rdr = myConn.cmd.ExecuteDataReader();
 and so on.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39233710
Ok, so it's basically useless to create an SqlConnection Class then if I still need to call the following below for every one of my 100+ pages??


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString);

MyConnection myConn = new MyConnecton(conn, spName);
sqlDataReader rdr = myConn.cmd.ExecuteDataReader();
and so on.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 39234502
basically, yes, since each page is essentially a separate 'stand-alone' mini-app.  Pages do not maintain 'state'.

AW
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 39237002
ages do not maintain 'state'
..but maybe that your SQL Server maintains a pool, so that different client connections from the same source are essentially served from the same server connection.
I would not worry about it...

Regards.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question