Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a assebly from 3 classes. For that one I'm using SqlConnection to deal with the sql database. Currently I'm hardcoding the ConnectionString and creating the sqlConnection inside the dataAccess class. But after creating the DLL user need to set the connection as he wants. What is the best way to design this thing.
Posted

If you are trying to use this as a general purpose control, I would pass the connection string to your code as part of the initilization: either via the constructor (preferred) or via a static instance. That way it is up to the application how and where he stores it, rather than the DLL. Since the App is more "aware" of the environment they will be running in than the DLL, that does make some sense!
 
Share this answer
 
Comments
SwitcherSoft 4-Apr-11 3:50am    
I Agree, and in the calling application you can store and encrypt it in the settings file.
You can use the Settings object, of the Configuration object, or design a solution yourself.

Try expanding the 'Properties' treeview item under your ptoject in the solution explorer, you'll see Settings.settings. You can add the connectionstring there and retrieve is in code. The other solution is to add a new file to your project (Application Configuration) and add the connectionstring in the ConnectionStrings section.
XML
<?xml version="1.0"?>
<configuration>
 <connectionStrings>
        <clear />
        <add name="Name" connectionString="ConnectionStringX" />
 </connectionStrings>
</configuration>


You must reference the System.Configuration library in order to call the ConfigurationManager.ConnectionStrings["Name"].ConnectionString which will return the connectionstring (in this case ConnectionStringX)

Have Fun!

Eduard
 
Share this answer
 
Comments
Sanyon_d 4-Apr-11 2:55am    
I'm already using this method inside the class. But after compiling as a classLibrary I can use the DLL only. Application configuration file will not come.
Eduard Keilholz 4-Apr-11 3:17am    
Have you considered creating a settings class which you serialize using XML?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900