I have a web application which i make on my local host and publish it on different servers.
in the web config of this application i have connectionstrings property like:
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=XYZ-PC\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
Now connectionstring data source has the name of my server and when ever i publish it and run this application on different server i have to change XYZ-PC\SQLEXPRESS to the name of the server..
Is there a way i dont have to do this as it does not feel right..
any suggestions..
thanks
Try replacing XYX-PC with localhost provided the instance name is the same.
If database stands on the same server as IIS, you can use Data Source=localhost\sqlexpress
I like to use configSource to pull the connection string out into a separate file, as explained here*: http://stevenharman.net/blog/archive/2007/06/07/tip-put-connection-strings-in-their-own-configuration-file.aspx
That way, you can configure each server's connectionStrings.config once, but continue updating their web.config files with a single version that works for all of them.
* Except, I usually name it connectionStrings.config, so it's more obvious for maintenance by others.
The following article could be a solution to your question :
Handling Multiple Environment Configurations with .NET 2.0
Basically, the idea is to use the fact that in your config file you can indicate that some sections have to be read from an external file. Than during the build of your project you copy the right external file according to your environment. I think the link will explain this better.
Take a look at Web Deployment projects.
In addition to letting you merge everything into a single assembly, it give you the option of doing section-based web.config replacements. We use this on our build server to alter the standard web.config for running in the test environment. As a bonus, you're not limited to just changing connection strings. Anything in the web.config is fair game.
Use this connection string:
name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"
It will work.
Related
This question already has answers here:
Using different Web.config in development and production environment
(10 answers)
Closed 5 years ago.
I have the source code of an application that I maintain. In my web.config file, I have connection strings for both my test and production databases as follows:
<add name="conn" connectionString="Data Source=TestDBServer; (etc...)">
<add name="conn" connectionString="Data Source=ProdDBServer; (etc...)">
Whenever I am in test, I just comment out the production connection string and vice-versa. For example, when I finish testing, I comment out the test connection string and uncomment the production connection string and then deploy.
I am very early on in my career as a developer, but it seems to me that there must be a better or more standard way to handle this. What should I do?
Edit
Guilherme Lofrano Corneto linked an identical question that completely answers my own question. I have since marked mine as a duplicate. Here is the link:
Change connection string from development to production when publishing
Visual Studio can automatically transform your web.config based on your currently active build configuration. So you should have a build configuration "Test", "Dev", "Prod" etc. (whatever you need for your workflow).
Then you can right-click on your web.config and click "Add transform". This creates a new config like web.Prod.config where you can overwrite your config values. When building with a specific build configuration, visual studio will automatically overwrite the default web.config with your corresponding transformation.
If you need to apply such transformations to other files than the web.config you might want to check extensions like "SlowCheetah - XML Transforms"
You can follow the following steps to transform web config file during deployment.
Here is a short summary:
1- Set your development connection string in the web.config file. Should look similar to this:
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=TestDBServer; (etc...)" />
</connectionStrings>
2- Expand your web.config file and open web.release.config
3- Use the Replace function to replace the connection string with the one for you want for deployment. You can use xdt:Locator="Match(name)" attribute to match it with the name of the connection string (ConnStringDb1) in this example:
<connectionStrings>
<add name="ConnStringDb1" xdt:Transform="Replace" xdt:Locator="Match(name)"
connectionString="Data Source=ProdDBServer; (etc...)" />
</connectionStrings>
More information on transformation syntax in web.config.
My team usually keeps the "dev" setup as the one in TFS as the default. It helps if all team members install the same SqlServer "instance" so if does not differ from dev to dev.
Example :
"localhost\SqlServ2014Stand
or something like that.
For deployments to QA, Staging, UAT, Production (whatever you call them)......we write wix installer(s) (one per product) and we handle the xml-transforms in the wix project. This is what will change the connection string (or anything else) for the different environments.
You may want to look at a CI server like CruiseControl.NET or Jenkins or TFS or something. This is what will build your code in a consistent way. This takes a little time to setup. If time if the main factor, I would try Jenkins.
I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :
<connectionStrings>
<add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string="data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.
You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:
Data Source=(local)\yourInstanceName
It's not a good idea to let your users change the connection string.
Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)
without use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
</connectionStrings>
</configuration>
local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.
one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.
requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.
in case you decide to use WiX you can modify your connection string like below.
<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]#name="PhoenixCacheEntities"[\]]/#connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>
you can see that an xpath is used here to determine the setting you want to change. hope this helps.
NOT RECOMANDED
its just becouse you said you are just starting in c#
so to write the connection string into a file :
using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");
to read From it :
using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");
btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become
File.Write("config.cfg",txtstring.Text);
PS : use the write when you want to save the CNX String like in the save button of that form .
i downloaded a demo application from http://code.msdn.microsoft.com/Authorization-based-aa2c0185/view/SourceCode
I run it on my Visual Studio and all worked fine. There is a database ASPNETDB.MDF in its App_Data folder which is used to store the user information or retrieve information for login purpose.
Now what I want is to change this database with another database in my SQL Server.
For that there can be two ways :
Locate the connection string etc. for ASPNETDB and change them with my own database.
or add this ASPNETDB file to SQL Server by attaching. But its showing error while attaching it.
Also if I exclude App_Data folder from project...Then even it works...
Where can find connection string for DB? I want to change the DB with mine own ?
Please Help
Thanks
It's using default connection string from machine.config:
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Go to the Web.debug.config file. There you will find comments noting how to wire up your config file. This will use config transforms so that when you build in debug mode, it will change your config file to override the machine defaults (which is what it is currently using). Just follow the comments and you should be good :)
I am using VSTS 2010 + ASP.Net + C# 4.0 to learn someone else's code for a WCF application. I find besides Web.Config, there are also Web.Debug.config and Web.Release.config. I searched the content of Web.Config, but cannot find any reference to Web.Debug.config and Web.Release.config. However in VSTS 2010 IDE solution explorer, I find there is an arrow pointed from Web.Config to Web.Debug.config and Web.Release.config -- so seems there is reference relationship. It makes me confused.
In all 3 config files, there are identical items with different values, for example, in web.config, there is connection string DBConnectinString defined in this way,
<connectionStrings>
<add name="DBConnectinString" connectionString="data Source=10.10.10.123;uid=foo;pwd=foo;database=FOO" providerName="System.Data.SqlClient"/>
</connectionStrings>
And in Web.Debug.config, there is connection string DBConnectinString defined in almost the same way with different values,
<connectionStrings>
<add name="DBConnectinString" connectionString="data Source=10.10.10.124;uid=foo;pwd=foo;database=FOO" providerName="System.Data.SqlClient"/>
</connectionStrings>
My quesiton is,
what is the relationship between Web.Config and Web.Debug.config/Web.Release.config?
Why define the same item with different values in Web.Config and Web.Debug.config/Web.Release.config?
You have different config files for different settings. Consider Debug as your local environment settings, like connectionstrings to the testserver, debug variables on, etc. The Release settings would contain settings like the connectionstring for the production server.
In the top bar, next to the run debug should be a drop down, containing all available settings. You can also add some.
This settings are useful for oneclick-deployment like the new WebDeploy with VS2010
Edit:
This link How to use web.config transforms to replace appSettings and connectionStrings? should show you a basic walkthrough about web.config transforms
This is new feature in Visual studio 2010. It allows you to have diffrent config files for you build configuration schemes. So that, when you build in debug mode it will include the Web.Debug.Config file, the same when you build for release.
This allows you for example to maintain diffrent configs for your database - one for your dev environment and for your live environment.
Hope that helps!
I have a system comprising 5 applications. Each app accesses a database via a DAL library. In the DAL i have an app.config with the following entry:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Using the full path for the attachDbFilename works fine. But I'm not happy:
I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
I dont want a full path, when it comes to deployment that aint going to work. Relative paths in the app.config do not appear to work. Ideally I would like to be able to pull the DAL from source control onto any computer and not have to worry about changing the connection string each time. This: http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx talks about |DataDirectory| for deployment purposes but this doesn't work for me in debug (unless I'm using it wrong, see 3)
This might be better as a separate question but it is related to 2. - Is there a "good" way of arranging multiple projects for debug? I have created a Bin dir and in each project settings I copy the dll/exe to this bin dir. I also have a copy of the database in here (I tried no path in the app.config but that didn't work either, nor did |DataDirectory|). Also incredibly annoying is that relative paths do not work in Debug\Working Directory setting either, so it looks like that is one place that would have to change each time code is checked out to a new machine?
Apologies for the war and peace and thanks in advance for any ideas.
Two answers - but not really full solutions:
1) I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
You could externalize the connection strings into their own config, something like:
<connectionStrings configSource="connectionStrings.config" />
and then have those connection strings in that new file:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
That way, you can have custom app.config's, but share the commonality.
2) I don't want a full path, when it comes to deployment that ain't going to work. Relative paths in the app.config do not appear to work.
Unfortunately, the only thing you can do here is to use the |DataDirectory| placeholder, which is a placeholder for the App_Data folder in an ASP.NET application.
<add name="DataAccessLayer.Properties.Settings.ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
The only other solution would be to use a server and connect to a server - rather than having files to dynamically mount and attach.
Using the connection string AttachDbFilename feature from multiple processes referring the same MDF is very bad idea. Sharing an auto-atached database can get you in all sort of complicated security/ownership database startup problems. And specifying User Instance=True is like pouring gas over the flame, since each user instance is per user so if your applications are ever configured to run under different apppool credentials or one suddenly is changed to impersonate, all hell breaks loose.
Just attach for good the MDF as an normal database to your SQL instance and use it as such, with a normall connection string: Data Source=.\SQLEXPRESS;Initial Catalog=<dbname>; Integrated Security=True.