I was wondering what is the difference in having connection strings in 2 app settings and connectionstrings? Just wanted to know technically in Visual Studio 2008 for windows authentication!
<appSettings>
add key="ConnectionString" value="Server=198.57.2.70;Database=SalesTracking;Integrated Security=SSPI" />
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="DataSource=198.57.2.70;InitialCatalog=SalesTracking;IntegratedSecurity=True;" />
</connectionStrings>
Thanks!!
The difference is that the connectionString section gives you strongly typed access to your connection strings through the ConfigurationManager class. It's meant for connection strings specifically.
AppSettings is meant to store general settings. You can use it to store connection strings, but I recommend not doing that since there is a specific element for it in connectionStrings
Related
I have a group of apps that inherit their connection string from a web.config in the root directory of IIS. This means that I don't need to specify a connection string in the root directory of the app, but I do need to specify a connection string when debugging locally.
My question is, how can I set a connection string in web.debug.config that only is used when I'm debugging?
You could specify the connectionstring in the normal web.config and then in the web.release.config add a transform that simply removes the connection string all together.
That way it should exist in the debug one but not in the web.release.config.
Of course this assumes you are using those configs with transforms and not doing a simple copy/paste of the code when you deploy
MSDN has a good example of this
The following example shows how to select all the connection string
add elements in the development Web.config file. In the deployed
Web.config file, only the first connection string element is removed.
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" />
</connectionStrings>
</configuration>
EDIT: I guess alternatively you could also create a transform in web.debug.config that adds it while in debugging, which might help to keep it out of the original web.config if you aren't appying transforms when deploying
Keep your Debug connection string in Web.Config and replace the release connection string in Web.Release.Config using the locator as show below
<connectionStrings>
<add name="DefaultConnection" connectionString="Release Connections tring" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/connectionStrings)"/>
</connectionStrings>
I am currently reading Manning's "ASP.NET MVC 4 in Action" book and trying to get the first example to work.
Within my test application, I built a simple Model and created some Views. I then imported "SQL Server Compact" using NuGet.
When I finally try to run the application I get the following error:
Invalid value for key 'attachdbfilename'
This occurs on every interaction with the Database (SELECT or other CRUD operations) I am running. Any ideas?
Though, I am bit late to respond to this question but I was facing the same issue and I resolved it by modifying the connection string like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Hope this would be helpful for others too.
I tried finding a solution for this particular error without success.
Fixed it in the end by updating the .Net Framework 4 to 4.0.2. Patch and details can be found Here is the link
Hope it helps
I think the problems here is string in web.config is not correct
According to your set up sql, the string will work if you set something similar like this
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Data Source=.\SQLEXPRESS; --> sometime, it will be Data Source=.;
after that, you need config right to access FOlder App_Data
If u test on Window 7, right click folder. property
Security tab -> add user network Service with full right
Go to section of Web.config and modify the section to the following to get SQLServerCE4 to be used:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
I guess problem occurs only when using SQL Express on .NET Framework v4.0.30319 anyways
SQL Server Express Local Database Runtime support in SqlClient is added in .NET Framework update 4.0.2 read - http://support.microsoft.com/kb/2544514
Alternately use connection string similar to this
<add name="EmployeeDbContext" connectionString="Data Source=.;Initial Catalog=Employees;AttachDBFileName=|DataDirectory|\Employees.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
above connection string was giving an error but as soon as added"\" before the name of my database the error got resolved.
Try this:
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
You are using SQL Server Compact 4.0:
Be sure you have installed SQL Server Compact 4.0.
Install VS 2010 SP1 Tools for SQL Server Compact.
Change your connection string to:
<connectionStrings>
<add name="MyModelDBContext" connectionString="Data Source=|DataDirectory|mymodel.sdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Right click on controllers folder and -> add -> controller
Type:
YourController
MVC-Controller with read/write ...
MyModel
new datacontext -> APP.Models.DatabaseContext
Here is a blog post I found about that error. It is a bit old, but uses the express version of sql server. MSDN Link
That blog post talks about expecting the server name of the sql database to be a local address and not /sqlexpress
There is also this blog post that talks about having an incorrect connection string. So maybe check your connection string to the database. and your problem could be there.
Facing the same issue, I have looked over my connection string and found that "UNC-style" paths e.g. \\MyServer\MyDBServer are not allowed, one has to use the MyServer\MyDBServer syntax for a remote host or the .\MyDBServer syntax for locally hosted DB Servers.
One database for the project and once for the authentication created by the MVC4 template.
Reply to Mystere Man:
I was following this MVC4 example:
It creates two connection strings in web.config file, one for forms authentication and the other one for product database.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ProductStore-20120829112625;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ProductStore-20120829112625.mdf"
providerName="System.Data.SqlClient" />
<add name="OrdersContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=OrdersContext-20120829131625; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|OrdersContext-20120829131625.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
No. It's not recommended, but it's not "not reccomended" either. You can put your membership tables anywhere you want. What's recommended is to put them where you want them.
But frankly, I see no valid reason to use a separate database, other than personal preference. I personally think it's pointless to put them somewhere else. Now you have to backup two databases and keep the backups synchronized.
Is it possible to change connection string (or just server host) relying on selected web publish profile? Maybe using Web.config transform or someway else?
I mean for profile "Test" change connection string "MyConnString" (in published Web.config) to "Data Source='example.com,14333;..." and for profile "Production" - to "Data Source=./SQLExpress;..."
This is exactly what web config transforms were created for. The link you provided in your post has a walkthrough of doing this specifically for connection strings.
To start with the transforms, right-click your web.config file in the project explorer and choose "Add Config Transforms". Assuming that you have ConfigA and ConfigB in your solution configuration, there will be two new files added, Web.ConfigA.config and Web.ConfigB.config.
If you open these new files, they'll be pretty empty except for a bunch of comments. They actually contain a connection string example in them that you can use though - it looks like this:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Uncomment this section, and change the "name" property to the name of the connection string in the base web.config file. Set the "connectionString" property to the actual value that you want to use for ConfigA. So, like this:
<connectionStrings>
<add name="myConnectionString"
connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Repeat the process for the Web.ConfigB.config file, with the desired connection string for ConfigB.
Now when you use the Publish command in visual studio, it will automatically transform the base web.config file, and set the "connectionString" attribute to whatever configuration you're in when you publish.
In one of the applications I have been referring to, the Connection String is stored in AppSettings! Till now I have been storing the connection in the <connectionstring/> element. But what is the correct way?
So my question is, what are the differences between <connectionstrings> and <appsettings> in the web.config and are there any specific reasons why I should or should not be storing connection strings in appsettings? Are there any rules / guidelines provided to follow? Or is this completely the choice of the developer?
There is a fundamental difference between connectionString and appSettings:
They look for different things. In .NET 2.0 and above:
A connectionString object is an XML node that has specific attributes to set; and semantically it refers to a database connection string.
For instance, a connectionString looks like the following:
<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
You'll notice it has a few different attributes:
name
connectionString : This has a specific string inside of it, it needs an Initial Catalog, a security mechanism (in this case Integrated Security
providerName
Whereas appSettings is just a user-defined Key-value pair that allows you to... well... set application settings. It can be anything:
<appSettings>
<add key="Mom" value="Your"/>
<add key="UseCache" value="True"/>
<add key="MapsKey" value="1234567890-AA"/>
<add key="SMTPServer" value="smtp.peterkellner.net"/>
</appSettings>
In many cases, it would just be odd to put the connectionString in a key-value pair like appSettings (semantically and programmatically). As well as it would make it more difficult to encrypt the connectionString when you need to.
There is more information about this from this blog post.
As far as I know it doesn't make a huge amount of difference, other than it being in the "correct" place - the main advantage of putting connection strings in their own section (you do encrypt your connection strings.. right?) is so you can encrypt that section without encrypting all of the settings you might want to change.
Connection strings are generally kept in the <connectionstring/> element...and is a good guideline since it's named properly.
Sometimes you might use a third party control or usercontrol that looks for the connection string in a key in the <appsettings> element. That should be the only exception to the guideline.
Additionally, in IIS7, connect strings can be maintained through the respective IIS7 Administration.
You can use appSettings section to share custom application configuration settings across projects in .NET.
How to share custom application configuration settings across projects in .NET
Regarding deployment, there's one significant difference between them. When you import web packages to IIS:
Connection strings will automatically be included in the wizard dialog for further parameterization.
App settings will not be there by default. If you really want to do that, please follow the steps in "Custom Parameterization - Application settings in the web.config file" section of Configuring Parameters for Web Package Deployment
That means, when it comes to deployment, you'd better put environment parameters(database, cache, AWS key/secret, etc.) in connection strings. It will provide differentiation between dev/staging/prod environment explicitly.