Click here to Skip to main content
Licence 
First Posted 8 Sep 2005
Views 73,711
Bookmarked 33 times

Sql Database Connection DLL Written in C#

By | 8 Sep 2005 | Article
Connect and close a sql database with internal error reporting using Event Logs
 
Part of The SQL Zone sponsored by
See Also

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.

string dbName = "testdb";

string serverName = "(local)";

string sapwd = "sapwd";
<PRE lang=cs>/// Using the standard constructor and inputing all information
/// From the constructor.  This will automatically instantiate the setConnection Member
 
private dbx32sql.Connect conn = new dbx32sql.Connect(dbName, serverName, sapwd);
/// 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

About the Author

Member 2260574

Web Developer

United States United States

Member

.Net programmer for 3 years. Owner of DotThisSoftware based out of Aberdeen, SD.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralConn open wait PinmemberAjay Kale New21:59 13 Feb '11  
GeneralMy vote of 1 PinmemberKarl Tarbet4:33 1 Dec '08  
QuestionIf I use ODBC? Pinmember22teddy228:07 24 Mar '07  
GeneralRight idea... PinmemberAl Einstien7:54 12 Sep '05  
GeneralOne of us has missed the point... PinmemberXendris9:04 9 Sep '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Sep 2005
Article Copyright 2005 by Member 2260574
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid