Using Dynamic Connection String in Asp.Net Webforms and Windows Forms

Asked

Viewed 155 times

1

I’m working on a project with 4 layers (Banco, RegraNegocio, Webforms and Windows Forms).

I made the following references:

Webforms > Regranegocio > Banco
winforms > Regranegocio > Banco

In my Class Library (bank) I have a file called: config.Settings

In this file I have only two properties of the type string.

strconnection - where the connection is stored.

infoBanco - where is stored which database is, e.g.: (access, sqlserver, postgre)

I assign values to these properties when login (winforms) is performed. They work perfectly, but when I went to use Asp.net makes a mistake, saying I can’t use the arquivo.settings.

I changed the scope of the User for application, but execution does not allow me to assign values to them at runtime. Properties have only {get;} when the scope is that of application.

Does anyone know what I can do to store the connection string? Because I access a database where the customer’s records are stored, I take the connection string inside the table, and then access the respective client’s database.

On desktop it was simple: stored in config.Settings and already leaves using methods and class because every time I send a command it opens the new connection using the property strConnection that I initialized when the user logged in.

  • The Connection string is not stored in web.config by default?

  • but I access other banks, the string is not fixed, I pick it up in a database and wanted to keep it in my class library bank with the config.Settings managed on desktop but on web I don’t know how to do..

1 answer

1

If you access the folder of your web application you will see that the file config.settings is not in the application directory. This is because references to other projects are copied to the folder bin as assemblies, but the physical files of the dependency do not.

The files must be replicated in the web environment manually (yes, you will have to create a copy) or you can put the information from this file in a Resource and when referencing the project on the web it will be available for your application Asp.Net.

My tip is to create a link to the file, so both projects will share a single file (you won’t need to update the information twice) follows a tutorial on how to do.

  1. Right-click on the root of your web project and go to: Add > Existing Item > Add as Link (Note that in the add button there will be a arrow down. Click on it to change from Add for Add as Link);
  2. Select the file config.settings exists in the directory of your database project and click OK;
  3. Note that a link to the original file will be created, ie both projects will share a single physical archive;
  4. Click on the linked file in the web application and go to Properties > Copy To Output Directory and assign the option Copy Always.

If you have any questions, feel free to comment that I will help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.