Page 1 of 1

Generic Database Library - Simple Tables A class library for working with sql Rate Topic: -----

#1 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Posted 23 January 2006 - 10:29 PM

This is a class that we developed at work to take care of the SQL work for us. This way you're working with objects rather than a bunch of SQL calls =) This doesn't work that well with complicated relational databases but works great for most simple projects.


The first step is to add a Application Config file to your project and then add the following line to it:

<connectionStrings>
    <add name="sqlConnectionString" connectionString="Data Source=Server;uid=username;pwd=password;Initial Catalog=database;"/>
</connectionStrings>



so that the GenericDatabaseLibrary will now what connection to use.

And then you create an empty class file and create the generic template of your database.

Here is an example on how to use this project:

public class MyFirstDbTable : GenericDatabaseTable
{
     protected override string TableName
     {	
          get	{    return "theTablesName;	}	
      }
      protected override TableColumn PrimaryKeyColumn
      {	
          get	{    return new TableColumn("primaryId", SqlDbType.Int); }	
       }
      protected override TableColumn [] Columns
      {
         get  {
                 return new TableColumn [] 
                 {
                       new TableColumn("strFirstCharColumn", SqlDbType.Char, 40),
                       new TableColumn("strSecondCharColumn", SqlDbType.Char, 40),
                       new TableColumn("intFirstIntColumn", SqlDbType.Int)
                  };
              }
      }
      
      // properties to access data
      public string FirstCharColumn
      {
     	 get { return (string)this.values["strFirstCharColumn"]; }
     	 set { this.values["strFirstCharColumn"] = value; }
      }
      public string SecondCharColumn
      {
     	 get { return (string)this.values["strSecondCharColumn"]; }
     	 set { this.values["strSecondCharColumn"] = value; }
      }
      public int FirstIntColumn
      {
     	 get { return (int)this.values["intFirstIntColumn"]; }
     	 set { this.values["intFirstIntColumn"] = value; }
      }
      // constructor to create an empty object
      public MyFirstDbTable() : base () { }
      
      // constructor to create an object based off of values
      public MyFirstDbTable(string one, string two, int three)
      {
     	 this.FirstCharColumn = one;
     	 this.SecondCharColumn = two;
     	 this.FirstIntColumn = three;
      }
      
      // constructor to retrieve an object from the database
       public MyFirstDbTable(int myId) : base(new TableIndex("primaryId", SqlDbType.Int, myId))	{  }
}
public class MainClass
{
      public static void Main()
      {
           // creates an empty object
         MyFirstDbTable ob = new MyFirstDbTable();
        // populates values
          ob.FirstCharColumn = "one!";
          ob.SecondCharColumn = "two...";
          ob.FirstIntColumn = 3;
        // inserts into database, and gets the Id 
          ob.InsertIntoDatabase(); // ob.Id now refernces the correct primary key
        // change information
           ob.FirstCharColumn = "four!";
        // update database
           ob.Update();
        // delete from database
           ob.Delete(); 	 // ob.Id should now be -1
       }
 }



Its a little weird to get started with but it make development a lot faster once you get used to it :) Post here or send me a pm if you have any questions!

Attached File(s)


This post has been edited by sontek: 24 January 2006 - 01:31 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1