Change the connection strings in MVC3 to publish in IIS - sql-server

I need to change the connection strings in my mvc proyect because is local and now I need publish in a server but I dont have idea how write the connection strings and they are the next
<add name="cnn" connectionString="Data Source=SISTEMAS-PC\SQLEXPRESS;Initial Catalog=FoodGroups;User ID=FoodGroup; Password=Food" providerName="System.Data.SqlClient" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(SISTEMAS-PC\SQLEXPRES)\v11.0;Initial Catalog=aspnet-MvcApplication1-20130730182253;Integrated Security=SSPI;AttachDBFilename=C:\Users\Sistemas\Desktop\proyectos TI\foodGroup2\foodGroup\App_Data" />
<add name="FoodGroupsEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SISTEMAS-PC\SQLEXPRESS;initial catalog=FoodGroups;user id=FoodGroup;password=Food;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

You have a web.config which should have a section for all your connection string.
What you need to do, is to have a web.release.config that will change the connection string when you build in release mode.
For example, web.config when you are debugging in your dev environment:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<connectionStrings>
<add name="YourConnectionString"
connectionString="Data Source=YourServer; initial catalog=YourDatabase; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configSections>
And inside the web.release.config
<?xml version="1.0"?>
<connectionStrings>
<add name="YourConnectionString"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
When you are building, you will see that the string "YourConnectionString" will be replaced. This way, you can deploy/publish your application depending of the build mode (debug or release) without having to modify your code and just relying on web.config.

Related

Web.Config SQL Connection

My Web.Debug.Config code:
<connectionStrings>
<add name="NextLevel"
providerName="System.Data.SqlClient"
connectionString="Data Source=DESKTOP-RTOUFCH\\SQLEXPRESS;Database=NextLevel;Integrated Security=True;" />
</connectionStrings>
My CSharp code:
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["NextLevel"].ConnectionString;
I keep getting a null reference error.
When I attempt to iterate through the connection strings, it finds one named "LocalSQLServer" which is not even in my web.Debug.config file.
What am I doing wrong?
That last comment by ThomasArdal:
I would also suggest you to publish web.config, web.debug.config, and web.release.config. If you have those files.
allowed me to figure out my problem.
I was updating web.debug.config when I should have been updating web.config.
<connectionStrings>
<add name="NextLevel"
providerName="System.Data.SqlClient"
connectionString="Data Source=.;Initial Catalog=NextLevel;Integrated Security=True;" />
</connectionStrings>
Check with this instead!

How to add Azure SQL Server connection string to app.config in Windows Forms?

I'm trying to add an Azure SQL Server connection string into my app.config file, but there are red underlines all over the connection string when I try to copy and paste it from Azure. I'm using Windows Forms in Visual Studio.
Here is my connection string:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="AddSales"
Server="tcp:Sales99.database.windows.net,1433;Initial" Catalog="Sales;Persist" Security="" Info="False;User" ID=""{your_username};Password=""{your_password};MultipleActiveResultSets=""False;Encrypt=""True;TrustServerCertificate="False;Connection" Timeout="30"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Is there a way to fix this issue? Please advise.
Your config is just plain wrong - you need to have an attribute connectionString in your config that contains all the details about the connection - something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="AddSales"
connectionString="Server=tcp:Sales99.database.windows.net,1433;Initial Catalog=Sales;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I also had this issue when I try to copy & paste connection string from the Azure database. This is connection string that worked for me.
Server=tcp:[serverName].database.windows.net;Database=myDataBase; User ID=[LoginForDb]#[serverName];Password=myPassword;Trusted_Connection=False; Encrypt=True;
This also works.
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=tcp:ServerName.database.windows.net,1433;Initial Catalog=DatabaseName;Integrated Security=False;User Id=username#servername;Password=password;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" />
If you need additional info, please visit these sites:
https://www.connectionstrings.com/sql-azure/
https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx

How to add password connection string

How to add password in below mentioned connection string
<connectionStrings>
<add name="Gate_App.My.MySettings.GateDbConnectionString1" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\GateDb.accdb;Persist Security Info=True "
providerName="System.Data.OleDb" />
</connectionStrings>
Use this
<connectionStrings>
<add name="Gate_App.My.MySettings.GateDbConnectionString1" connectionString=" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\GateDb.accdb; User ID=*******; Password='*******';Persist Security Info=True "
providerName="System.Data.OleDb" />
</connectionStrings>
<connectionStrings>
<add name="Gate_App.My.MySettings.GateDbConnectionString1" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=|DataDirectory|\GateDb.accdb;Persist Security Info=True;User ID=myUsername;Password=myPassword;"
providerName="System.Data.OleDb" />
</connectionStrings>
Access connection strings
Try this,
<add name="Gate_App.My.MySettings.GateDbConnectionString1" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\GateDb.accdb;User Id=id;Password=pass;" providerName="System.Data.OleDb" /

how to change connection string entity framework in runtime

how to change connection string entity framework in runtime
code in my appconfig file :
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
I want to change the following code :
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=127.10.10.23,1356;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
just data source changed.
How is it done?
When you instantiates your context it has parameter connectionString. You can change it there.

How to connect to DB on SERVER for Asp.Net membership?

I've just upload my ASPNETDB.mdf using "Generate Scripts" into server.
The problem is, I don't know how can I connect to it for my Membership.(e.g LogIn Controls)
where is the ConnectionString?
update the Data Source tag in web config
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=<<PATH Goes Here>>;Persist Security Info=False;
The connectionstring is in the web.config file.
Modify the web.config file for your application to redefine the LocalSqlServer connection string to point to the database.
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="server=.;database=aspnetdb;
integrated security=sspi;"/>
</connectionStrings>
If your Membership provider is not pointing to "LocalSqlServer", then you can modify that in the web.config as well.
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, ..."
connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="7"
minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
/>
</providers>
</membership>

Resources