database in asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuananh87vn
    New Member
    • Sep 2007
    • 63

    database in asp.net

    hi,
    I'm absolutely newbie to asp.net and i've just started working with database. each time I want to make a database query, I have to start the connection by creating a new sqlConnection object. Is there anyway to keep the connection with the database, err, how to create the dataset or datasource for the whole project, so that I don't have to repeat the above action again and again. does it have anything to do with web.config file?

    In php, it can easily be done by create a config.php storing db configuration then include it in all other files. what about in asp.net?

    thanx
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    A wonderful observation that is ignored by most people who post in these forums. In fact you should never mix SQL and asp code. Better create some DB access classes and call methods from those classes. It's the basis of the MVC.

    For the connections, you should have a GetConnection() method that returns a connection from some connection pool.

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      you may find these articles useful
      Database P1
      Database P2

      Comment

      • tuananh87vn
        New Member
        • Sep 2007
        • 63

        #4
        so, may i use this way:
        in a file db_class.cs i create a class dbconnection:

        Code:
        class dbConnection
        {
        // returns the connection to sql database
        public SqlConnection getConnection() {
                // connect to sql database
                string sqldb = WebConfigurationManager.ConnectionStrings["testdb"].ConnectionString;
                SqlConnection connectDB = new SqlConnection(sqldb);
                return connectDB;
        }
        
        // open the connection
        public void openDB() {
               this.getConnection().open();      
        }
        
        // close the connection
        public void closeDB() {
               this.getConnection().close();      
        }
        
        // other method...
        //end of dbConnection class
        
        }

        then I include this class file into other files each time i need to interact with db, and just call the method getConnection() to get connection object (used for sqlCommand object), and openDB() the activate the connection and so on...

        am i going the right way? actually i don't know how to include a class file into other files (ow, my bad) (which is similar to include('filena me.inc') in php)

        i'd be grateful if anyone can correct me and show better ways :)

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You can just use that class in another class withing the the same Namespace. If you decide to use that class in a different Namespace then you can import it using the using keyword. To create a complete model layer you'll need to do a bit more than that. Google DAO for a start.

          P.S Wash your mouth next time you say "PHP" <washes mouth>

          Comment

          Working...