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.
Related
Several ASP.NET applications are running on the server of our company.
Connections strings in web.config look like this:
<add name="somename"
connectionString="Data Source=SERVER\INSTANCE; Initial Catalog=DbName;User Id=user; Password=*******; MultipleActiveResultSets=True; Integrated Security=true"
providerName="System.Data.SqlClient"/>
and work perfectly with no errors.
But when I copy this connection string from web.config to app.config of my WPF app, I get the following errors:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Or (if I remove Integrated Security from string)
Login failed for user 'username'.
Also I cannot log in via SQL Server Management Studio (causes the same errors).
My app.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="somename"
connectionString="Data Source=SERVER\Instance;Initial Catalog=DbName; User Id=user;Password=******;MultipleActiveResultSets=True;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
Both username and password are 100% correct, SQL Server uses mixed authentication.
Managed to log in with sa login of SQL Server
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
Add a DWORD value called “DisableLoopbackCheck”
Set this value to 1
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
I am trying to export my database as .Net SqlClient Data Provider format but I keep getting this error message
The operation could not be completed
ADDITIONAL INFORMATION:
The ConnectionString property has not been initialized. (System.Data)
Program Location:
code:
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Dts.DtsWizard.DTSWizard.GetOpenedConnection(WizardInputs wizardInputs, String connEntryName)
at Microsoft.SqlServer.Dts.DtsWizard.Step2.OnLeavePage(LeavePageEventArgs e)
but my app config has the connection string declared as
code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsApplication2.My.MySettings.ConnectionString"
connectionString="Data Source=ARULJUSTIN\SQLEXPRESS;Initial Catalog=firemaintain;Integrated Security=True;Pooling=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
</configuration>
have you tried this?
<connectionStrings>
<add name="ConnectionString" connectionString="data source=ARULJUSTIN\SQLEXPRESS;Initial Catalog=firemaintain;Integrated Security=True;Pooling=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
and to access it try using
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
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.
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>