Steven
Steven

Reputation: 19435

Need generic function(s) for connecting to DB and retrieve data

I'm making a DAL class which I can use to connect to DB and retrieve data. I'm using SQL Server 2005 Express (and Visual Web Developer 2008 Express Edition).

I found several examples on the web for connecting an retrieving data. But none where made inn to a class object.

This is kind of a pseudocode I've put together. Can anyone help me with some code which I can use to get data from MS DB?

namespace development.DAL {

    public class myDAL
    {
        SqlConnection conn;
        string conStr = "myConnectionString";

        public myDAL()
        {
            string connStr = Config.Get(this.conStr);
            this.conn = new SqlConnection(connStr);
        }

        // Function for retrieving data from DB
        public DataSet GetAllRows(string table)
        {

            string sql = string.Format(@"
                SELECT  *
                FROM    '{0}';
                ", table);

            DataSet dbDataSet = Command.CreateDataSet(cmd);  //Pseudocode!
            return dbDataSet;
        }
    }
}

Upvotes: 0

Views: 138

Answers (2)

Paul Sasik
Paul Sasik

Reputation: 81509

Steven, There are also code generators out there that will create a complete DAL layer for you. Often the procedure is as simple as pointing the code generator at a db, selecting your tables and clicking go...

Check out: http://www.mygenerationsoftware.com (free, open source and my current fave) http://www.codesmithtools.com (solid, professional, no longer free but with free trial)

And there are literally dozens of others.

Upvotes: 1

Lazarus
Lazarus

Reputation: 43094

Search online for the repository pattern, I think that's what you are looking for. It'll help you abstract the data storage from the actual mechanics of accessing the database. Additionally, you'll find it easier to test and debug (or at least I do).

Upvotes: 0

Related Questions