I have a developed a application in c#,asp.net web application(using only inside our company) and finally am deploying the application in server PC (different host and port )allocated for this purpose.
When i developing or maintaining the application in my PC, having different host and port, every time when i need to publish im changing the connection string in web.config and and copying app folder in server PCs, interpub->wwwroot.
Is there any option to avoid each time changes? i.e, for publishing i will use seperate web.config and for developing work i use seperate web.config.
Server PC- web.config, connection string:
<connectionStrings>
<add name="MySqlConnectionString" connectionString="server=192.168.3.5;user id=root;password=sim;database=simpayroll;allowuservariables=True;port=3306"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="server=192.168.3.5;user id=root;port=3306;password=sim;database=simpayroll;persistsecurityinfo=True;allowuservariables=True"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
My local Web.config
<connectionStrings>
<add name="MySqlConnectionString" connectionString="server=localhost;user id=root;password=root;database=simpayroll;allowuservariables=True;port=3306"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="server=localhost;user id=root;port=3306;password=root;database=simpayroll;persistsecurityinfo=True;allowuservariables=True"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
You can use web.config transformations to over come with your problem
In your Web.Release.Config file,
<connectionStrings xdt:transform="Replace">
<add name="MySqlConnectionString" connectionString="your connection string"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="your connection string"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Related
I am referencing 2 databases in ASP.NET using Entity Framework.
In my web.config file, I can see the connection strings for the 2 databases:
<connectionStrings>
<add name="RContext"
connectionString="metadata=res://*/Models.RModel.csdl|res://*/Models.RModel.ssdl|res://*/Models.RModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="CEntities"
connectionString="metadata=res://*/Models.CModel.csdl|res://*/Models.CModel.ssdl|res://*/Models.CModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Can I somehow implement alternate connection strings where the datasource refers to the prod server for the release?
This is typically handled with web.config transforms.
In your project you would have:
web.config
web.Release.config
For example in your web.Release.config transform you would have something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="RContext"
connectionString="RContext-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="CEntities"
connectionString="CEntities-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
You'll notice the xdt:Transform="SetAttributes" xdt:Locator="Match(name)" bit, which says, in the main web.config find the connectionString by name and replace its attributes with the ones defined here.
This will automatically happen when you publish the application.
I have ASP.NET MVC project.
In WebConfig file I have a connection to local db.
Here is code:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=DESKTOP-SV2O11U;Initial Catalog=RIS_Main;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="RIS_MainEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-SV2O11U;initial catalog=RIS_Main;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
But the problem in that I working on two pcs, work and home.
So it has a different name.And I need to change DataSource in DefaultConnection
Is there any variant to keep 2 pcs name in connectionStrings?
There are two ways to solve it:
If your database server is on the same host as your application server, you can access the database via localhost host.
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=RIS_Main;Integrated Security=True" providerName="System.Data.SqlClient" />
The other way is to have multiple transformations of the same config file (like you already have with Debug and Release).
Using this way, depending on your build configuration you can have multiple completely different connection strings. Visual Studio will decide during build which one to acutually use.
Image Source
I have an ASP.net web application hosted in IIS. This application use SQL Server.
I removed connection string in web.config. But when I start the application, it is still connected to it.
The connection string was nowhere explicitly stated.
how does it work?
By default the machine.config located in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config have the following connection string configured.
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
By leaving the <connectionStrings> section of your web.config empty you are not removing the default connectionstring. To ensure that you have no configured connection string you should use a <clear /> element in your web.config file.
<connectionStrings>
<clear />
</connectionStrings>
I have an app in Azure Web Apps (ASP.NET MVC 4) and want to upload files to Azure Storage. The Web.config is configured with the Storage Emulator:
<appSettings>
...
<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
<add key="CloudStorageContainerReference" value="dnc-demo" />
</appSettings>
And the connection string is configured in the Web.Release.config :
<connectionStrings>
<add name="StorageConnection" connectionString="DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=XXX" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
The problem is that when I upload the file(in production) the following error occurs:
[SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:10000]
How can I fix it??
Thanks!!
FIX:
Web.config should have the following configuration:
<connectionStrings>
<add name="StorageConnection" connectionString="UseDevelopmentStorage=true" />
</connectionStrings>
And the connection string should be this way in Web.Release.config:
<connectionStrings>
<add name="StorageConnection"
connectionString="DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=XXX"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
So the CloudStorageAccount can be initialized this way:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ToString());
I have connected from my PC to my instance RDS (MSSQL Server Express) using Managment Studio without problems, because I have already configured the policy groups and other stuff.
My "simple" problem is when I try to connect from my WebApp, I have a web.config and this is my configuration in ASP.NET MVC 5 Project... very easy:
Can you help to configure my connection string correctly?
Thanks!!
<add name="Entities" connectionString="metadata=res://*/Business.Interactive.Model.csdl|res://*/Business.Interactive.Model.ssdl|res://*/Business.Interactive.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com:1433;Database=Business.interactive.gci;User Id=userroot;
Password=XXXXXXXX;pooling=False;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="myBusinessContext" connectionString="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com:1433;Database=Business.interactive.gci;User Id=userroot;
Password=XXXXXXXX;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Ok, The problem was the length of the name of the database and I also I had to delete the number port:
Finally:
<add name="Entities" connectionString="metadata=res://*/Business.Interactive.Model.csdl|res://*/Business.Interactive.Model.ssdl|res://*/Business.Interactive.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com;Database=Businessgci;User Id=userroot;
Password=XXXXXXXX;pooling=False;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="myBusinessContext" connectionString="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com;Database=Businessgci;User Id=userroot;
Password=XXXXXXXX;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Regards!!