Solved

ADO.NET SqlConnection

Posted on 2013-06-02
8
247 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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