Answered by:
Adding a connection string in a class library

Question
-
User-184840539 posted
I am looking to create a class library that contains classes I want to share across multiple web applications. I am trying to read the data from an SQL server in the library class but I don't have access to the system configuration manager to read connection string. How can I keep a dynamic connection string in my class library? Can I reference the library and then read the connection string from web.config and pass it back to the library.
I am thinking of having a base class where I define a function called ConnectionString such as
public string ConnectionString(){
string str = ""; return str;}
So in the library class I would use the following code:
string qry;qry =
"SELECT * FROM [TABLE] WHERE ([ID] = " + ID + ");"; SqlConnection cn = new SqlConnection(); cn.ConnectionString = this.ConnectionString();cn.Open();
Is it possible to do this using the library class??
I am hoping there is a way to set the Connection String for the whole library in the Web App with a single function...
Thanks
Saturday, March 7, 2009 4:43 PM
Answers
-
User807886609 posted
Your class library should be able to access the ConfigurationManager.ConnectionStrings section of the web.config as long as it has a reference to System.Configuration namespace. There should be no need to hard-code any connection strings.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 7:08 PM -
User-1910946339 posted
Something like this
public class MyLibraryClass { private string connectionString; public MyLibraryClass(string connectionString) { this.connectionString = connectionString; } public something SomeMethod(int param) { SqlConnection cn = new SqlConnection(connectionString); cn.Open(); ... } }
When your web applications wants to use the library they pass the connection string to the constructor
MyLibraryClass lib = new MyLibraryClass(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; somthing x = lib.SomeMethod95); ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 10:48 PM -
User-1171043462 posted
string qry;qry =
"SELECT * FROM [TABLE] WHERE ([ID] = " + ID + ");"; SqlConnection cn = new SqlConnection(); cn.ConnectionString = this.ConnectionString();cn.Open();
Instead you should use parameters as this can lead to sql injection
refer here it might also contain what you need else reply back
http://www.aspsnippets.com/post/Parameterized-Queries-ADONet.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 11:21 PM -
User-319574463 posted
If you look at the CommonParam project at http://www.CodePlex.Com/CommonParam, there is code to read a connection string from an XML file.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, March 8, 2009 3:36 PM -
User-1630302068 posted
I am trying to read the data from an SQL server in the library class but I don't have access to the system configuration manager to read connection string.You need to add a reference to the System.Configuration assembly from the GAC.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 9, 2009 10:13 AM
All replies
-
User807886609 posted
Your class library should be able to access the ConfigurationManager.ConnectionStrings section of the web.config as long as it has a reference to System.Configuration namespace. There should be no need to hard-code any connection strings.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 7:08 PM -
User-1910946339 posted
Something like this
public class MyLibraryClass { private string connectionString; public MyLibraryClass(string connectionString) { this.connectionString = connectionString; } public something SomeMethod(int param) { SqlConnection cn = new SqlConnection(connectionString); cn.Open(); ... } }
When your web applications wants to use the library they pass the connection string to the constructor
MyLibraryClass lib = new MyLibraryClass(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; somthing x = lib.SomeMethod95); ...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 10:48 PM -
User-1171043462 posted
string qry;qry =
"SELECT * FROM [TABLE] WHERE ([ID] = " + ID + ");"; SqlConnection cn = new SqlConnection(); cn.ConnectionString = this.ConnectionString();cn.Open();
Instead you should use parameters as this can lead to sql injection
refer here it might also contain what you need else reply back
http://www.aspsnippets.com/post/Parameterized-Queries-ADONet.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 7, 2009 11:21 PM -
User-319574463 posted
If you look at the CommonParam project at http://www.CodePlex.Com/CommonParam, there is code to read a connection string from an XML file.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, March 8, 2009 3:36 PM -
User-184840539 posted
Thanks, for everyone's help.
Monday, March 9, 2009 7:38 AM -
User-1630302068 posted
I am trying to read the data from an SQL server in the library class but I don't have access to the system configuration manager to read connection string.You need to add a reference to the System.Configuration assembly from the GAC.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 9, 2009 10:13 AM