Handle two connection for multiple PC's (ASP.NET MVC) - c#

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

Related

Published website: an attempt to attach an auto-named database for file...failed

I'm trying to publish a website with a locally stored database on my localhost. The problem is that I can't enter into any page who have a database embedded as a data source. All databases are kept into the App_Data folder, and I've used both into attachdbfilename:
1. |DataDirectory|/Database1.mdf
2. C:/Users/username/Desktop/websitefolder/App_Data/Database1.mdf
I've tried the other topics to solve my problem, but they are not mentioning that's a published website. The site works without any problem on localhost with the port assigned, but any page with a database/source related shows this error.
There is my config file:
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf; User Instance=false;Integrated Security=false" providerName="System.Data.SqlClient" />
<add name="connect" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database2.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Try adding the "InitialCatalog=DatabaseName;" to your connection string:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"
Here are some examples from the MSDN website to refer to.

I have two web.config files. Which one should I use?

I recently built an application using visual studio express. This application was built using a local DB provided by VS express. I now have a subscription (free trial) to Microsoft Azure. What changes do I need to make to my project in order to point my project to the newly created Azure DB?
I also noticed I have 2 web.config files. one in the root directory and one in the views folder.
I believe I have to make an update to my connection string, but in a specific config file or both?
Here is my root directory web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20160922101748.mdf;Initial Catalog=aspnet-MvcMovie-20160922101748;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and my (views) web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Any help would be highly appreciated.
I believe only one web.config with connection strings should be enough. Keep the one in the root.
Download Azure DB connection strings from the Azure portal and replace connectionString values in the web.config file with the Azure DB connection string(s).
You might also need to whitelist your machine's IP address in the Azure Portal to allow connections to Azure DB.
You only need to change the web.config on the root folder as thats the one used by the application.
And you are right, you need to point the connection string to your Azure database.
You definitely only need to update the one web.config in the root of the application with the connection string. The other web.config in the views folder is needed for your views to use html helpers and to make routing work as you expect in an mvc application so do not remove it.

Restless connection to the Entity Framework

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>

How to configure connection string to RDS Instance (MSSQLServer Express)

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!!

How to change database that with web configuration is created?

How to change database that web configuration is created?
When I use Asp.net Configuration, visual studio create a default data base (ASPNETDB.MDF),
I want use my database for my web site .
How to do this?
Just add this tab in <connectionStrings> of your Web.Config
<connectionStrings>
<add name="YourConnectionStringName"
connectionString="DataSource=yourDataSource;Initial Catalog=YourDataBaseName;User ID=YourID;Password=YourPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
You should a add database using this line in web.config
replace with aspnetdb connection string
<connectionStrings>
<add name="LoanCalculatorConnectionString" connectionString="Data Source=servername;Initial Catalog=dbname;Persist Security Info=True;User ID=888;Password=8888"
providerName="System.Data.SqlClient" />
</connectionStrings>
To this porpus use this way:
C://windows/Microsoft.net/Framework/v2.0.50727/aspnet_regsql.exe
. is run
Next--> Configuration SQL server for application service --> select server and database --> Next --> Finish

Categories