3

I am New to the more enhanced features of the C# and .NET coding practices

I have been trying to Find A solution to using a particular method in C# for Winforms, Windows Services And ASP.NET web Applications where you program your connection to SQL and Convert the C# Code file to a dll to be used in your project.

The idea is to Create the Connection and Convert it to a dll so that every time you start a function and need to make a database connection you will just write it where you say

Function ABC
{
    //VB Version would be like this as i have seen this
    Dim DCDLL as New Dataconn 

    //Where 'DCDLL' is the DLL file which is being declared as a new dataconnection

    //C# Version would be Alon these Lines
    SQLConnection DataConn = New SQLConnection(DCDLL)
}

I have only seen the VB Code version of the Call so I am not keen on the C# Method that this would be done

Meanwhile The DLL Holds all the other code like

string ConnectionString = 
                    "Data Source=Datasource; UID=User; PWD=Pass; Persist Security
                     Info=True; Initial Catalog= Catalog";
Dataconn = new SqlConnection(ConnectionString);
Dataconn.Open();

the purpose of the DLL would be to handle the connection to the database, Open it catch errors if dataconnection is not successfull and close the connection if needed so that you dont have to programatically do this every time. Also it is only responsible for the connection therefore you can use a function to call the connection execute the procedure and whatever else is required.

The idea of using a DLL is just to make the Connection settings to the Database a little more secure, the Connection obviously wont become super secure but it adds more security than having the Code in you code pages etc.

i have spoken to and seen people use this type of method but my research on how to achieve this via google and other sources does not seem to understand what i am searching for.

I am trying to understand that when i code this file how it must be done as to ensure that it handles the database connection correctly without issues and doesnt break.

If anyone can Give me Examples in C# of how to do this it would be appreciated or if you know of any pages that have explained how to achieve this i would be most grateful for your assistance

XenitXTD
  • 59
  • 1
  • 6
  • 2
    I think your question is more about basic organization of code into assemblies and classes than about anything SQL connection related. With regards to security, splitting the database code into a separate assembly will add no additional level of security over just doing it in the main assembly. – Mike Marynowski Oct 19 '12 at 11:35
  • Google for things like "C# OOP Tutorial" and you should get a ton of resources to help you design your code to be split into different classes and assemblies, i.e. http://cplus.about.com/od/learnc/ss/csharpclasses.htm. If you split it into a different class and it works, you can then just move that class to another assembly. – Mike Marynowski Oct 19 '12 at 11:39

1 Answers1

2

You should put your connection strings in app.config / web.config, and use c# to get the values...

Get ConnectionString from app.config in c#

You can also encrypt the connection string...

http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx

Community
  • 1
  • 1
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82