Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,542 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,672 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Generic Database Library - Simple Tables

 
Reply to this topicStart new topic

> Generic Database Library - Simple Tables, A class library for working with sql

sontek
Group Icon



post 23 Jan, 2006 - 10:29 PM
Post #1


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:

CODE

<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:

CODE

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 smile.gif Post here or send me a pm if you have any questions!

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


Attached File(s)
Attached File  GenericDatabaseLibrary.zip ( 53.17k ) Number of downloads: 511
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/7/08 05:32PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month