Different connection string for each publish profile in VS2010 - asp.net

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.

Related

Make the connectionstring's AttachDbFilename relative in config file

I am working on a project using an mdf file generated locally using Entity Framework Code First. The path to this mdf is set in several config files in my solution using <connectionStrings> sections like so :
<add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="E:\path\to\project\app_data\local.mdf";Integrated Security=True" providerName="System.Data.SqlClient" />
I use git versionning on this project both from work and at home, thus at work the mdf filepath has to be E:\path\to\project\app_data\local.mdf\ and at home D:\otherpath\to\project\app_data\local.mdf.
This is painful to change everytime I comute (first world problem, I know).
I have seen how to set a substitution string but this seems to be using code outside the config file and I don't want that. Maybe there is a way to set a relative |DataDirectory| value inside the config file ?
Can I make this path relative to a unique place next to my .sln file, using only those config files ?
This would ideally be something like that :
<add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="|RelativeToWorkplaceDynamicPath|\local.mdf";Integrated Security=True" providerName="System.Data.SqlClient" />
Thanks.
I think I figured out how to make this work.
I explain in detail at: How to embed a database in a visual studio solution?
In short, you start your connection string with the substitution string "|DataDirectory|". And then you set your current AppDomain's "DataDirectory" setting to where you want it to be, before you access the database.
It's not perfectly clean, but it's workable.

Difference between connection strings in App Settings and connectionStrings configuration setting

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

Connectionstring to other projects' App_Data folder

I have a asp.net webforms project with a database in the App_Data folder referenced by connectionstring:
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
The solution also contains another project with webservices that need to access the same database.
Is it possible to write a relative path to this database in the connnectionstring? - eg. something like this(does not work):
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|/../../ProjectName/App_Data/database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
Basically |DataDirectory| is an alias for App_Data folder. Since you are not using App_Data in your services project, it would not help you in this situation.
What you can do though is try starting from the project root using ~\:
AttachDbFilename=~\..\ProjectName\App_Data\database.mdf
Also there is always an option to use an absolute path:
AttachDbFilename=d:\Projects\ProjectName\App_Data\database.mdf
And finally there is always an option to build connection string programmatically.

Not able to uploade website on server

I am try to uploading website.I don't know how configure database(web.config file) on uploading time.I am using Somee.com , anyone familiar with somee.com Or know how to configure database the please help me.
webconfig file code
<configuration>
<connectionStrings>
<add name="abc" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jai-Ganesh\Documents\Visual Studio 2010\WebSites\testinmg\App_Data\Database.mdf;Integrated Security=True;User Instance=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<customErrors mode="RemoteOnly" ></customErrors>
</system.web>
</configuration>
Note: I don't know what is path of the dataSource in server.
Link of the page:
[http://www.imureditor.somee.com/add_value.aspx][1]
first you need to contact your hosting provider for connection string. They will give you the ip address (you can also the host name) which you can then configure it.
See the exmaple below;
<add name="connectStringName" connectionString="Data Source=somee.com;User ID=yourusernameforDB;password=yourpasswordforDB;Initial Catalog=yourDBName;" />
OR take a real example below;
<add name="KenSchoolConnectionString" connectionString="Password=1234;Data Source=192.168.1.1;Integrated Security=False;Initial Catalog=KenMISSchoolDB;User ID=sa" />
OR
Go to connectionstrings.com
Your connection string references your local SQL Server instance and tries to attach a local file; this connection string will need to be changed to reflect the setup on the server, so you'll need to specify the Data Source, Catalog, User ID and Password - Integrated Security won't work, and attaching *.mdf files from App_Data probably won't either.
Your service provider is responsible for giving you the credentials needed for this, we can't guess them.

Can I specify a connection string in the web.debug.config only?

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>

Resources