Click here to Skip to main content
Click here to Skip to main content

A Way to Easily Connect to a SQL Server or an Access Database

, 29 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An asembly that provides a way to retrieve, store, and manipulate data in a SQL Server or Access database.

Introduction

Often times, I have found myself in a situation where I wanted to interact with a database, be it MS SQL Server, SQL Express, or Access. My enthusiasm that came with me when starting to work on a new application would be shattered right from the start, when I was to face the "connection string". And this was only the beginning; things would get worse when I had to perform even the simplest task, such as running a Select statement. And when I had to run a Stored Procedure, I knew from the start I had to spare some time in advance.

The project...

Not anymore. After running into the same annoying things over and over again, I decided to put together an assembly that took care of all the details for me. Here are the main features:

  • The connection string is history. Now, all you have to do, once you decide to give my code a try, is to specify the server name, database name, username and password. You can choose to specify the connection type (in the case of SQL Server, but if you don't, the assembly will decide which one to use based on whether or not you specified a username).
  • SqlDatabase db = new SqlDatabase(); 
    db.ConnectionString.Server = @"(local)"; 
    db.ConnectionString.AuthenticationType = AuthenticationTypes.Sql; 
    db.ConnectionString.Username = "myusername"; 
    db.ConnectionString.Password = "mypassword";
  • You can check whether the server connection is available or whether a database, a table, or a Stored Procedure exists, by calling a single method.
  • if (db.CheckServerConnection()) 
    { 
        if (db.CheckDatabaseExists("MyDatabase")) 
        { 
            //... 
        } 
        else 
        {
            //...
        } 
    } 
    else 
    {
        ...
    }
  • You can create or drop a database directly from C#, and know whether the action was successful or not.
  • The feature I like the most is the ability to call Stored Procedures without having to bother too much about the parameters. Here's how to do it:
  • ds = db.RunStoredProcedure("getPromotion", Helpers.CreateSqlParamater("@name", "p1") );
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        Console.Write (ds.Tables[0].Rows[i]["id"] + " "); 
        Console.WriteLine(ds.Tables[0].Rows[i]["name"]);
    }

You can create a SQL parameter or an Access parameter by calling the corresponding static function, to which you specify the parameter name, as it will be passed to the Stored Procedure, and its value. The function is overloaded to accept some basic parameter types. After that, you need to pass these parameters to the Stored Procedure function call, and you're done! Now you can process the data set.

Another useful method for SQL is called RunScript, and with it, you can run a SQL script, just as you would from SQL Query Analyser. The transactions must be separated by the word "GO" (on a new line), just like in Query Analyser.

Conclusion

I hope you all like this as much as I do and, if you have any suggestions on how I can improve the code, please let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ovydiu

Romania Romania
No Biography provided

Comments and Discussions

 
Questiondatabase connectivity Pinmemberkk_upadhyay9-Jul-07 0:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 29 Jun 2006
Article Copyright 2006 by ovydiu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid