Solved

ADO.NET SqlConnection

Posted on 2013-06-02
8
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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