How i can change connection string after publishing the project? - c#

When i change connection string after publish project in visual studio it is giving me an error
Here is my app.config file
<connectionStrings>
<add name="DataContext" connectionString="metadata=res://*
/Model.DataContext.csdl|res://*/Model.DataContext.ssdl|res:
//*/Model.DataContext.msl;provider=System.Data.SqlClient;provider connection string="data source=CODE-SERVER\SQLSILENT;initial catalog=pos-standard;persist security info=True;user id=sa;password=abc123**;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
and in new app.config file i want to replace this connection string it my local database connection string.
<add name="DataContext" connectionString="metadata=res://*/Model.DataContext.csdl|res://*/Model.DataContext.ssdl|res://*/Model.DataContext.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=pos-standard;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I also attach here a error window

Are you sure that your database authorises Integrated security ?
Can you connect using SQL server management studio with integrated security ?
If yes, try to not replace all the connection string but only remove username and password ant put integrated security = true, and change server name.
Finally, check the database server name may be .\Something.

You need to re-sign the application and deployment manifests after you have edited the config file. Please refer to the following links for more information about this.
http://msdn.microsoft.com/en-us/library/dd465299.aspx
Resign Clickonce manifest using mage.exe

Related

Get SQL Connection error while using Web Service with local SQL Server database

I'm setting up a web service, I wanted to use the local database so I created a local SQL Server database in this project. The web service runs just fine on localhost, but the problem when I invoke a method to Load data from the local database on this project, I'm getting an error:
System.Data.SqlClient.SqlException:
An attempt to attach an auto-named database for file C:\Program Files (x86)\IIS Express\TourTravelDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
TourTravelDB.mdf is my local database
I've added a connection string in my web.config but it's still not working.
Here is my connection string.
<connectionStrings>
<add name="CS"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TourTravelDB.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I've also tried to move my database to the IIS Express directory, it works but when run it from another computer, I have to move the database again.
Does anyone have an idea how to solve this?
This may help you..
Add "User Instance=True;" in your connection string.
<connectionStrings>
<add name="aspnet_staterKits_Test_TimeTracker"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
</connectionStrings>
if doesn't work then try out the below links..
forums.asp.net
codeproject.com
Already existing thread in stack overflow

EF 6 "Unable to load the specified metadata resource" -- but I'm not using edmx

Sorry if this has been asked, but I've gone through all of the other posts I can find and everyone seems to be using an EDMX, and not a "production" database.
I get the following error:
System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource
when attempting to access my application. My connection string is as follows:
<add name="FormAssemblyEntities" connectionString="metadata=res://*/Models.Entities.FormAssembly.csdl|res://*/Models.Entities.FormAssembly.ssdl|res://*/Models.Entities.FormAssembly.msl;provider=System.Data.SqlClient;provider connection string="data source=server\instance;initial catalog=dbname;user id=userid;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
This works perfectly if I am running it locally and it connects to the edmx with the following connection string, but as soon as I put it on our UAT server and point it to the database, I get that error.
<add name="FormAssemblyEntities" connectionString="Data Source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\FormAssembly.mdf;Initial Catalog=FormAssembly;Integrated Security=SSPI;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
I really have no idea where to start. The database appears up to date, the user has access, and the server has access to the SQL server.
I have also tried replacing the res://*/ with res://FormAssembly/ and res://FormAssembly.dll/, but the former results in the same error, and the latter says the dll can't be found (the DLL is in the bin directory).
Any help would be appreciated.
Thanks,
Travis
In my case, the metadata part of my connection string was malformed.
The following connection string worked in my case:
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=Northwind;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>

Connecting to mdf database with Web.config ASP.NET

I tried many things and saw a lot of information on the internet. It just doesn't work and I want to know what am I doing wrong.
I created a .mdf database in my App_Data folder and trying to connect it through Web.config.
my <connectionString /> looks like this:
<add name="Datab1" connectionString="Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|Datab1.mdf;Database=Datab1; Trusted_Connection=Yes;" />
And I also tried this:
<add name="Datab1" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=E:\Secret\Secret\App_Data\Datab1.mdf;
Integrated Security=True;
User Instance=True"/>
The providerName="System.Data.Client" />gives me an error so I removed it.
What am I doing wrong here?
I finally fixed it by going to Server Explorer > Connect to Database > Data source to "Microsoft SQL Server Database File (SqlClient)" and Db file name browse to the .mdf file you want to use. Use Windows Authentication.
in the Web.config use this string:
<add name="Datab1" connectionString="Data Source=(localdb)\v11.0;AttachDbFileName=path\to\folder\of\database\Datab1.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
And replace Datab1 with your own database name of course. This worked for me so this question is solved!

Login failed while connecting to database

I have created WPF application which has MDF file. When I added MDF file to my solutions folder It automatically created a connection string for me
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
I Deployed my application using SQL Server Compact (as described here). But When I run my Application I am getting exception
Cannot open database "C:\Users\Public\Desktop\MyDatabase.mdf" requested by the login. The login failed.
Login failed for user 'MY-PC\NAME'.
I am using Windows Authentication for my SQL Server
change your connection string as follows:
<add name="Connection"
connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=C:\Users\Public\Desktop\MyDatabase.mdf;persist security info=True;Integrated Security=SSPI;"providerName="System.Data.SqlClient" />

Entity Framework connection string is being rewritten when website is published

When publish a MVC website via Visual Studio it rewrites the connection string for Entity Frameworks with an invalid one.
It appears to be the same invalid one it created when I first imported the database model (not code first) The correct connection string is in my local web.config, and there are no changes being made by the web.release.config file.
Correct connection string:
<add name="Entities" connectionString="metadata=res://*/Models.MODModels.csdl|res://*/Models.MODModels.ssdl|res://*/Models.MODModels.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=R4S-MOD;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Generated connection String:
<add name="Entities" connectionString="Data Source=localhost;Initial Catalog=R4S-MOD;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.EntityClient" />
When you do a release publish they should show you a popup where you choose if you want to do filesystem or iis deployment. there is a section in the wizard which has connection string. check it there, if there is already a preselected value for your connection string – qamar

Categories

Resources