setting connection parameters in web config - c#

I want to know if it's possible to set a connection in a web config of my web project that depends of a dll and not from the connection String.
UPDATE
Ok I have a dll, that make's a connection with my database, i want to know if it's possible to configure my web.config to get the connection from the dll and not from the string connection.

This is only possible if you edit the web.config in your code to set the connection based on your dependency at run-time.

Yes - in fact it's the proper way to do it. You simply have teh DLL read the application's Web.config:
ConfigurationManager.ConnectionStrings["MyConnectionString"];
Where MyConnectionString is:
`<connectionStrings>
<add name="myConnectionString" connectionString="<your connection string>" providerName="<provider name>" />
</connectionStrings>`
EDIT TO ADD
I answered before the edit to your question. However, I still question your approach. The whole purpose of a DLL (in my experience) is to encapsulate code that can be used by multiple applications. The application calling the DLL should be responsible for setting the connection string.
However, if there's some reason that you must have the connection string in the DLL (hardcoded, I assume?), you would need to expose it (like via a property) so that consumers of the DLL would have access to it. In that event, the application could use the property to get the connection string.
Stephen Kinningham's answer is probably the route to go for what you appear to be attempting, but honestly, I can't think of a reason off the top of my head why you would want to.

<connectionStrings>
<add name="MyConnection" connectionString="Data Source=SQLSERVERNAME;Initial Catalog=MYDBNAME;Persist Security Info=True;User ID=USERID_OR_ADMIN;Password=PASSWORD;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Related

Get Azure connection string in datalayer

Someone thought, a long time ago, it was a good idea to add connections string hardcoded in the datalayer of our web api. Because of this legacy I cannot remove this class. This class inherits IDbContextFactory and it needs to retrieve a connection string, which now is hard coded. To make it more... dynamic, I want to use the Azure connection strings for this.
1 I added the connection string to the configuration of the app service
2 According to a lot of websites I can just add the following code to retrieve the connection string:
connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
This does not work; it gives a "Object reference not set to an instance of an object." error.
Other say you need to add a prefix and the environment:
connectionString = Environment.GetEnvironmentVariable("SQLCONNSTR_DefaultConnection");
This does not work either, obviously. And yes, I selected sql server in the dropdown.
Other thing I tried, as someone suggested, is to add the connection string to the web.config. And again; this didn't work.
Good to know is that this is .NET 4.6.2, so all the beautiful solutions for .NET Core 1/2 aren't going to work.
First you have to include the same connection string in the web.config with empty connectionString value. Then you can overwrite from the application settings section.
`<connectionStrings>
<add name="DefaultConnection" connectionString="" />
<add name="CMSEntities" connectionString=""
providerName="System.Data.EntityClient" />
</connectionStrings>`
For entity framework specify providerName as "System.Data.EntityClient" and settings chose Custom instead of SqlServer

Regarding missing connection string in web.config

I am learning basics of MVC. And forgive me If I am not able to ask question properly because I am not aware of various technical terms. Anyways I am trying build one simple page where I will have two tables courses and Instructors. I am able to do most of the stuff and it seems to be working. Also I do have a solution given by my faculty of the same problem. When I am comparing my solution and faculty solution then in the web.config file of the actual solution given by my faculty I see something like
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcCRUDwithSQL-20140217025002.mdf;Initial Catalog=aspnet-MvcCRUDwithSQL-20140217025002;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="CourseContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CourseContext-20140217145250; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CourseContext-20140217145250.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
Where as in my solution that I just have one add name tag. I don't know why the another tag did not get created automatically. Or do I need to add that tag manually.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication9-20150313042831.mdf;Initial Catalog=aspnet-WebApplication9-20150313042831;Integrated Security=True" providerName="System.Data.SqlClient" />
Have I done something wrong that the connection string related to CourseContext did not get generated automatically. Or we need to add for CourseContext manually? Please help me. Since most of the files and its content are generated automatically I must have done something wrong that connection string pertaining to CourseContext did not get generated.
Use the steps below to generate the connection string via visual studio server explorer
Click on server explorer
Connect to database
Choose server name from dropdown
Choose your authentication type
Select your database from dropdown
Test connection
Copy the connection string from the properties and paste in your web.config
Also take a note of #Guffa answer, he's got a very good point
You don't need one connection for each table, you only need one connection for each database. A database can contain many tables (the project I'm working on right now has 9 tables, and that is a really small project).
The Visual Studio template starts out with a default connection string, but no database. It looks like it's that connection string that is still left in the solution.

Changing ConnectionString authorization method cause migration fail

I've created project to read from MSSQL DB and display some data. VS's wizards created connection string like the following:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
When I tried to deploy application to the real web server I changed connection string to use faceless DB account:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;User Id=userid;Password=password; MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
It looks like EF thinks that there some migration needed and tries to execute a lot of meaningless operations including attempts to execute SQL expressions with schema creating.
I have no write permissions on this DB server and I don't see any reasons to allow EF change DB structure.
So I'm completely lost there. What is the proper way to change authorization methods without changing on DB side?
Try to change connection string to production in VS project, then use Update-Database via Package Manager Console.
Pretty strange for me but the root of the issue was in default "initial catalog". It was stored somewhere for connection with integrated security but when I changed authorization method default was broken. As soon I defined initial catalog it was fixed.

Getting database connection error in three tier architechture in mvc 3

I'm trying to implement basic three tier architecture with mvc 3. but getting an error while running the solution ( showing no error while building the solution). The organization of my projects & error message can be seen in the attached image.
Please help.
OK guys, This is an update for.
As specified by some comments, I copied the original connection string from app.config to my web.config file & getting this error as shown in the image.
I tried to search a lot but no clue! Any help will be highly appropriated.
#Maarten is right. you always copied the normal connection string (that is a part of entity-connection string). you need to copy the entire connection string that was generated in your App.config file in Data layer and paste it in the web.config file in your presentation layer. the entity-connection string like as :
<add name="NorthwindEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=Northwind;persist security info=True;user id=sa;password=Blister01;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
don't copy-paste it, it is just a sample
The connection string you've copied is a normal connection string. You are using an edmx file which means you need an entity-connection string.
You need to copy the entity-connectionstring into the web.config. You can find the EF-connectionstring in the app.config file in the Data layer where EF added it for you.
you just add connection string connectionStrings section in web.config in Presentation layer!
See this image
EDITED: try with this
<add name="NORTHWINDEntities" providerName="System.Data.SqlClient" connectionString="data source=(local)/sqlexpress; initial catalog=NORTHWND;Integrated Security=SSPI" />

Connection string for Fluent Nhibernate with MySQL

I've reviewed the post How to configure Fluent NHibernate with MySQL, but I'm relatively new to MySQL and I need to actually set up with connection string itself. I've installed MySQL as part of a WAMP install and need to fill in the actual connection string. Can someone help me by extending the linked answer to actually contain a full connection string example?
Appreciated.
Edit: I've tried several different things and I keep getting the following error message:
Can't load file FluentConfiguration.cs under d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg.
Check the file permission and the existence of that file.
I installed FNH via nuget, and I don't understand why it's looking at that path, as the d: drive is my CD and not a harddisk. Very confused.
The error you've pasted looks like Visual Studio trying to localise the sources to show you where the exception comes from. This is not the real exception message - you should have it somewhere there and it's probably something about wrong configuration.
If you've installed WAMP with default settings, it is configured to listen on 3306 port and have only local root account without password. So your connection string should look somehow like that:
Server=localhost; Port=3306; Database=[database_name_here]; Uid=root; Pwd=;
(Pwd= part may be not needed at all).
So you need to paste it in your App.config/Web.config's <connectionStrings> section:
<connectionStrings>
<add name="ConnectionString"
connectionString="Server=localhost; Port=3306;
Database=[database_name_here]; Uid=root; Pwd=;"
providerName="System.Data.SqlClient" />
</connectionStrings>
and then use the solution from linked question:
MySqlConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey("ConnectionString")
)
Alternatively, you can paste the connection string directly in your Fluent's configuration, like this:
MySqlConfiguration.Standard
.ConnectionString.Is("Server=localhost; Port=3306;
Database=[database_name_here]; Uid=root; Pwd=;")
Anyway, this default root/no password configuration can be used only for local development and testing purposes.

Categories