Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, March 7, 2009 4:43 PM
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
All replies (6)
Saturday, March 7, 2009 7:08 PM ✅Answered
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.
Saturday, March 7, 2009 10:48 PM ✅Answered
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);
...
Saturday, March 7, 2009 11:21 PM ✅Answered
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
Sunday, March 8, 2009 3:36 PM ✅Answered
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.
Monday, March 9, 2009 10:13 AM ✅Answered
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.
Monday, March 9, 2009 7:38 AM
Thanks, for everyone's help.