Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi Everyone,
I want to convert my code to wrapper class so i can call it from all the project, and don't need to keep repeat it .

this is part of my code right now. i want to wrap the open connection ,DataTable
C#
protected void Page_Load(object sender, EventArgs e)
    {
        this.Master.HighlightMenu = "Customers";
        //Declare the connection object
        SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);

        //Make the connection 
        Conn.Open();

        //Define you query
        string sql = "SELECT * FROM Customer Order by LastName, FirstName";

        //Declare a SQL Adapter
        SqlDataAdapter da = new SqlDataAdapter(sql, Conn);

        //Declare a DataTable
        DataTable dt = new DataTable();

        //Populate the DataTable
        da.Fill(dt);

        //Bind the Listview
        lv.DataSource = dt;
        lv.DataBind();

        dt.Dispose();
        da.Dispose();
        Conn.Close();
    }


I tried like that but i don't know if it right or how to call it from any page.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public class DB
{
    private SqlConnection sqlConn; 
	public DB()
	{
	}
public void OpenConnection()
{
    if (sqlConn == null)
    {
      sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
           sqlConn.Open();
    }
	
	else 
	{
		 sqlConn.Close();

	}

}

public DataTable RunQuery(string Sql)
{
    OpenConnection();
    
}
Posted
Updated 29-Mar-12 3:48am
v2

C#
public class DB
    {
        public static SqlConnection Connect()
        {
            string connectionString = "Data Source=.;Initial Catalog = YourDatabaseName;uid =sa;pwd = YourPassword";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // Do something
                return connection;
            }
            
            
        }
    }


Good introduction to this topic:
http://msdn.microsoft.com/en-us/library/x9afc042.aspx[^]

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx[^]

And the rest of the C# Programming Guide.

Regards
 
Share this answer
 
v3
Comments
Mike988 29-Mar-12 10:05am    
Thank you so much .i did this but how can i recall it from the page , it's first time i use wrapper.
El_Codero 29-Mar-12 10:09am    
Do you mean how to invoke (call) it from another page (class)?
DB.Connect();
Mike988 29-Mar-12 10:14am    
thank you so much
El_Codero 29-Mar-12 10:18am    
You're welcome.
Best Regards
Small example can be found here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900