Click here to Skip to main content
15,895,142 members
Articles / Database Development / SQL Server
Article

Sql Database Connection DLL Written in C#

Rate me:
Please Sign up or sign in to vote.
1.05/5 (13 votes)
8 Sep 20051 min read 110.1K   2.3K   35   5
Connect and close a sql database with internal error reporting using Event Logs

Introduction

An easy to use DLL written in c# for opening and closing a database.  This DLL has an internal Event Logging in the event that the DLL encounters an error.  The event log will be put into source db error.  Below is a list of Private, public delegates and members.

Public Delegates

SqlConnection Connection - Returns the actual connection to the database.

String Database - Sets or returns the string value for the database name.

String Server - Sets or returns the string value for the server name.

String Sapwd - Sets or returns the string value for the sql authentication password.

Public Members

IsOpen() - Determins the connection state of the connection.  Returns true if open, false if closed.

Open() - Opens the database.  Returns true if it is opened properly.  False if there was an error.  Then logs error in Event Log.

Close() - Closes the database.  Returns true if properly closed. False if error.  Logs error in event log.

setConnection() - Sets the connection string for the SqlConnection to m_conn.
(To use this Member you must either have constructed the Database through first constructor or you must have submitted individual delegates as explained above.)

Private Members

errTrack() - Sets a new Error Log entry and makes the log an error for event viewer.

MessageError() - If there was an error it would show a message box explaining that there was an error and will forward them to event viewer.

 

This is a straight forward approach for database connecting.

To use this you have one of two constructors to use. Please see example execution below.

C#
string dbName = "testdb";

string serverName = "(local)";

string sapwd = "sapwd";
C#
<PRE lang=cs>/// Using the standard constructor and inputing all information
/// From the constructor.  This will automatically instantiate the setConnection Member
C#
private dbx32sql.Connect conn = new dbx32sql.Connect(dbName, serverName, sapwd);
C#
/// Using the non standard constructor with no inputs. Must initiate setConnection;

private dbx32sql.Connect conn = new dbx32sql.Connect();
conn.Database = dbName;
conn.Server = serverName;
conn.Sapwd = sapwd;
conn.setConnection;

 
try
{
    if (conn.Open)
    {
        string sqlGet = "SELECT COUNT (ID) FROM authors";
        SqlCommand cmdGet = new SqlCommand(sqlGet, conn.Connection);
        int Count = (int)cmdGet.ExecuteScalar();
        CmdGet.Dispose();
    }
    else
    {
        // Input your error handling here;
    }
}
catch(Exception ex)
{
    //insert other error handling.
}
finally
{
    conn.Close();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
.Net programmer for 3 years. Owner of DotThisSoftware based out of Aberdeen, SD.

Comments and Discussions

 
GeneralConn open wait Pin
Ajay Kale New13-Feb-11 21:59
Ajay Kale New13-Feb-11 21:59 
GeneralMy vote of 1 Pin
Karl Tarbet1-Dec-08 4:33
Karl Tarbet1-Dec-08 4:33 
QuestionIf I use ODBC? Pin
22teddy2224-Mar-07 8:07
22teddy2224-Mar-07 8:07 
GeneralRight idea... PinPopular
Tad McClellan12-Sep-05 7:54
professionalTad McClellan12-Sep-05 7:54 
GeneralOne of us has missed the point... Pin
Xendris9-Sep-05 9:04
Xendris9-Sep-05 9:04 

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

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