locked
Create and Call a class for sqlconnnection RRS feed

  • Question

  • User-1282880130 posted

    Hi Friends,

                     I have an sql connection in all pages of my web application project.I had used this line of code in each page for connection,

    SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=dbname;Integrated Security=True");

                  I want to reduce my code..I have the same connection in all of my pages..How can I create a class for sqlconnnection?..and how can I call the connection from each of my page?..Thanks in advance

    Thursday, August 5, 2010 1:31 AM

Answers

  • User-1618234021 posted

    Ideally, there should be a complete class or lib that will be responsible for data operations including opening connection, closing, select data, insert data etc. But for your scenario, you can create a simple class like the following:

    public class DAL
        {
            public SqlConnection getConnection()
            {
                try
                {
                    SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=dbname;Integrated Security=True");
                    con.Open();
                    return con;
                }
                catch (Exception ex)
                {
                    return null;
                }
    
            }
        }


    Now you can call this method in any page like:

    DAL d = new DAL();
    SqlConnection con = d.getConnection();
    //do the operations with con
    con.close();


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 5, 2010 3:26 AM

All replies

  • User-1618234021 posted

    Ideally, there should be a complete class or lib that will be responsible for data operations including opening connection, closing, select data, insert data etc. But for your scenario, you can create a simple class like the following:

    public class DAL
        {
            public SqlConnection getConnection()
            {
                try
                {
                    SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=dbname;Integrated Security=True");
                    con.Open();
                    return con;
                }
                catch (Exception ex)
                {
                    return null;
                }
    
            }
        }


    Now you can call this method in any page like:

    DAL d = new DAL();
    SqlConnection con = d.getConnection();
    //do the operations with con
    con.close();


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 5, 2010 3:26 AM
  • User-1282880130 posted

    Thank u very much..

    But "A field initializer cannot reference the non static field...." error is shown in the line

    SqlConnection con = d.getConnection();   

                 why this happen? how can I solve?..

    Thursday, August 5, 2010 4:30 AM
  • User178517217 posted

    then declare that function as the static function , or create an object for that class and using the object call the function

    Thursday, August 5, 2010 5:14 AM
  • User-1618234021 posted

    I have tested the code that I posted, it is working fine. It seems that you are trying to access the method from a static context or the declaration of the class has some problems.

    Can you please post the whole code, including the definition of the class, where you are calling the method. Full code will help to diagnose the problem.


    Thursday, August 5, 2010 5:16 AM
  • User-1282880130 posted

     

    Thank u verymuch..Now it is ok

    I write like this

      Connection d = new Connection();

    SqlConnection con;

    con = d.getConnection();

     

    Thursday, August 5, 2010 5:41 AM