Solved

ADO.NET SqlConnection

Posted on 2013-06-02
8
238 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now