Connection to database

Discussion in 'ASP .Net' started by RedRed, Mar 6, 2005.

  1. RedRed

    RedRed Guest

    Hi all,

    Hi to connect to database if I want to write the code in
    the class instead in the html (.aspx). What I want to do
    is to create a reusable class which allow the other page
    to connect using one class.

    Thanks all
     
    RedRed, Mar 6, 2005
    #1
    1. Advertising

  2. I think you have answered this yourself -- add a class to your project,
    which connects to the database, and perhaps returns a connection object that
    all pages can re-use.


    // Define a class
    public class MyConn
    {
    // This is the internal member to hold connection
    SqlConnection _conn = null;

    // Create a connection in the constructor
    public MyConn()
    {
    // Create a connection in this constructor
    }

    // This is the public property
    public SqlConnection Connection
    {
    get
    {
    return _conn;
    }
    }
    }

    Hope that helps.

    --
    Manohar Kamath
    Editor, .netWire
    www.dotnetwire.com


    "RedRed" <> wrote in message
    news:4e0201c52249$8997a160$...
    > Hi all,
    >
    > Hi to connect to database if I want to write the code in
    > the class instead in the html (.aspx). What I want to do
    > is to create a reusable class which allow the other page
    > to connect using one class.
    >
    > Thanks all
     
    Manohar Kamath, Mar 6, 2005
    #2
    1. Advertising

  3. Hi,

    As Manohar pointed out, you can add a class to your project and proceed.


    Another way would be to create a wrapper class for the methods provided by
    the framework. Make an assembly (a .dll file) and reference it. This is in
    case you have a 3 tier architecture and needs to access the data access
    methods a lot..

    A sample can be like this...

    Create a static class like DataBase and have a private static connection
    string

    private static string sConnectionString = The connection string

    You can then have methods like this for filling up datatable / dataset. You
    can also have for ExecuteNonQuery and any others you need.

    public static DataTable ExecuteDataTable(SqlCommand command)
    {
    using (SqlDataAdapter da = new SqlDataAdapter(command))
    {
    command.Connection = GetConnection();
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
    }
    }

    Just another approach...

    HTH,

    Need any help, do post a msg back..


    Happy coding

    "RedRed" wrote:

    > Hi all,
    >
    > Hi to connect to database if I want to write the code in
    > the class instead in the html (.aspx). What I want to do
    > is to create a reusable class which allow the other page
    > to connect using one class.
    >
    > Thanks all
    >
     
    =?Utf-8?B?VmlzaG51LUNoaXZ1a3VsYQ==?=, Mar 6, 2005
    #3
  4. Good idea, also: Instead of using a static variable, store the connection
    string in web.config to make it portable, and if need be encrypt it using
    asp setreg utility.

    --
    Manohar Kamath
    Editor, .netWire
    www.dotnetwire.com


    "Vishnu-Chivukula" <> wrote in
    message news:...
    > Hi,
    >
    > As Manohar pointed out, you can add a class to your project and proceed.
    >
    >
    > Another way would be to create a wrapper class for the methods provided by
    > the framework. Make an assembly (a .dll file) and reference it. This is in
    > case you have a 3 tier architecture and needs to access the data access
    > methods a lot..
    >
    > A sample can be like this...
    >
    > Create a static class like DataBase and have a private static connection
    > string
    >
    > private static string sConnectionString = The connection string
    >
    > You can then have methods like this for filling up datatable / dataset.

    You
    > can also have for ExecuteNonQuery and any others you need.
    >
    > public static DataTable ExecuteDataTable(SqlCommand command)
    > {
    > using (SqlDataAdapter da = new SqlDataAdapter(command))
    > {
    > command.Connection = GetConnection();
    > DataTable dt = new DataTable();
    > da.Fill(dt);
    > return dt;
    > }
    > }
    >
    > Just another approach...
    >
    > HTH,
    >
    > Need any help, do post a msg back..
    >
    >
    > Happy coding
    >
    > "RedRed" wrote:
    >
    > > Hi all,
    > >
    > > Hi to connect to database if I want to write the code in
    > > the class instead in the html (.aspx). What I want to do
    > > is to create a reusable class which allow the other page
    > > to connect using one class.
    > >
    > > Thanks all
    > >
     
    Manohar Kamath, Mar 6, 2005
    #4
  5. That sounds fine.

    I'd be wary of calling it a "wrapper", though,
    to prevent confusion between that and Interop.




    Juan T. Llibre
    ASP.NET MVP
    http://asp.net.do/foros/
    Foros de ASP.NET en EspaƱol
    Ven, y hablemos de ASP.NET...
    ======================

    "Vishnu-Chivukula" <> wrote in
    message news:...
    > Hi,
    >
    > As Manohar pointed out, you can add a class to your project and proceed.
    >
    >
    > Another way would be to create a wrapper class for the methods provided by
    > the framework. Make an assembly (a .dll file) and reference it. This is in
    > case you have a 3 tier architecture and needs to access the data access
    > methods a lot..
    >
    > A sample can be like this...
    >
    > Create a static class like DataBase and have a private static connection
    > string
    >
    > private static string sConnectionString = The connection string
    >
    > You can then have methods like this for filling up datatable / dataset.
    > You
    > can also have for ExecuteNonQuery and any others you need.
    >
    > public static DataTable ExecuteDataTable(SqlCommand command)
    > {
    > using (SqlDataAdapter da = new SqlDataAdapter(command))
    > {
    > command.Connection = GetConnection();
    > DataTable dt = new DataTable();
    > da.Fill(dt);
    > return dt;
    > }
    > }
    >
    > Just another approach...
    >
    > HTH,
    >
    > Need any help, do post a msg back..
    >
    >
    > Happy coding
    >
    > "RedRed" wrote:
    >
    >> Hi all,
    >>
    >> Hi to connect to database if I want to write the code in
    >> the class instead in the html (.aspx). What I want to do
    >> is to create a reusable class which allow the other page
    >> to connect using one class.
    >>
    >> Thanks all
    >>
     
    Juan T. Llibre, Mar 6, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mythran
    Replies:
    5
    Views:
    4,990
    Mythran
    Oct 5, 2005
  2. Dave Johnson

    Using Connection Class VS Connection Module

    Dave Johnson, Dec 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    654
    Dave Johnson
    Dec 11, 2005
  3. Linus Nikander
    Replies:
    5
    Views:
    557
  4. Replies:
    3
    Views:
    40,577
    Roedy Green
    Nov 16, 2005
  5. R
    Replies:
    1
    Views:
    587
Loading...

Share This Page