Connection string sharing

I am developing a class library (C #) which I will use for my various projects (later). My library dll library will use a project connection string / data line that will reference my new dll. How can i do this?
Suppose I have a class library project called "CLP" and a website project "WP". I can add a link to the CLP.dll file, but how do I pass the connection / data string object to this DLL? since CLP.dll will access db based on the connection string "WP".

Not sure if my problem is clear or not!

+3
source share
2 answers

If you are creating your class library and it requires a connection string called "ConnectionString", if the project you are calling it has a connection string in the configuration file of the Web application "ConnectionString", then this should be fine.

So use your project names. Your CLP class project with a data access code will establish a connection using the string "ConnectionString":

_serverConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

and then you encode this connection.

In the web.config file in your web project ("WP"), you add the following:

  <connectionStrings><add name="ConnectionString" connectionString="Data Source=.\SQL2008;Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>

Obviously, pointing to the data source, etc. that you are using for the WP project.

Hope this helps.

+1
source

Just add @WestDiscGolf answer:

( "ConnectionString" ).

: DLL :

public void buildConnection(string sConnectionString){
   //Some code, variables, etc

   _serverConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[sConnectionString]);

   //Some more code, etc etc

}

:

buildConnection("MyConnectionStringNameInTheConfigFile");

** . void , , . , , .

0

Source: https://habr.com/ru/post/1739977/


All Articles