Correct app.config connection string for application using LocalDb and DbContext? - connection-string

I have a Windows Forms application that uses LocalDb and DbContext. This works fine on my development box but I have not been able to get it to work when deployed to another box.
In my app.config, I instantiate a named LocalDb instance in with:
<system.data.localdb>
<localdbinstances>
<add name="LocalProvergience" version="11.0" />
</localdbinstances>
</system.data.localdb>
My connection string in is:
<add name="LocalProvergienceEntities"
connectionString="metadata=res://*/ProvergienceModel.csdl|
res://*/ProvergienceModel.ssdl|
res://*/ProvergienceModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=(localdb)\LocalProvergience;
initial catalog=LocalProvergience;integrated security=True;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
If I include:
AttachDbFilename=|DataDirectory|\LocalProvergience.mdf;
in the connection string, I get an "Invald key value for attachdbfilename" error.
If I exclude AttachDbFilename, I get "A network-related or instance-specific error occurred while establishing a connection to a SQL server" error.
Several questions:
The providerName is set to "System.Data.EntityClient" should this be "System.Data.LocalDb"?
Would anyone be able to post or direct me to an example of a correct app.config for LocalDb and DbContext?

When debugging what does AppDomain.CurrentDomain.GetData("DataDirectory") equal when running the app? If null you can set it with
AppDomain.CurrentDomain.SetData(
"DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));
I just ran into this, using but my AttachDBFilename = |DataDirectory|database.mdf, I think it doesn't need the extra \ because of the Path.Combine.
Also I notice the v11.0 property Auto-Create when running SQLlocalDB info, is Yes, not sure if that makes a difference when specifying named instances.

Related

Why connection string is not working in production asp.net?

I am trying to connect my application with a database on the server for production but it is throwing an error like this:
ERROR:A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
In the web.config I had the connection string for development that worked perfectly but when I changed it for production, it is not working. Here is what I did:
<connectionStrings>
<add name="Vulosjet"
connectionString="Data Source=IP;Initial Catalog=Vulosjet;Integrated Security=false;User ID=********;Password=********" />
<add name="Vulosjet_app.Properties.Settings.VulosjetConnectionString"
connectionString="Data Source=IP;Initial Catalog=Vulosjet;Integrated Security=false;User ID=********;Password=********"
providerName="System.Data.SqlClient" />
</connectionStrings>
Any help please?
You're missing the providerName in your Vulosjet connection string.
Use only one connection name and you can change the content via your deployment method. Think of it as a variable with different values depending on where it's deployed. Read about "web.config transformations" to see how you achieve that.
Second, make sure that you can access the db server from your production environment.
Sort out these things and you'll be good to go.
There are many possible reasons:
Sql Server instance name is different (not MSSQLSERVER which is the default) - you would need to specify this in the server name e.g. 10.10.42.53/SQLProd or something
What protocols are configured for SQL on the Prod server? You may need to enable TCP/IP if you want to connect that way...
There is a firewall issue, either software on the SQL server in Prod or somewhere in the network in between the two.
There is a basic networking problem - there is no direct route for the PROD web server to talk to the PROD SQL server.
What this isn't (yet) is an authentication issue. it just can't see the server/instance

How can I change my connection string to point to SQL Server instead of SQLEXPRESS?

created a project with Web Developer Express 2012 using SQLEXPRESS as the database. I’ve deployed to a shared hosting site and I’m trying to use the connection string provided to me. I was given this:
Data Source=tcp:TheDatabase.com;Initial Catalog=TheNameOfTheDatabase;User ID=Username;Password=******;Integrated Security=False;
Here is how I’m using it:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=. TheDatabase.com;Initial Catalog= TheNameOfTheDatabase;User ID=Username;Password=******;Integrated Security=False; ;User Instance=true"
providerName="System.Data.SqlClient" />
I get this error message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
All the research I did points to my connection string trying to access an instance of SQLEXPRESS only I don’t see any where in my string where this is happening. How do I make it point to SQL Server instead of SQLEXPRESS?
Is there somewhere else in web.config that may need changing?
I know this would be easy to fix if I could use IIS to manage the database but my ISP is blocking port 1433 and I can’t connect directly to the site’s database. Very frustrating.
UPDATE"
I think my problem is in the Computername/Instance section of the string. Can someone please tell me which part of my string would be the computername and the server instance? On my local machine it is MyPcName/SQLEXPRESS or .\SQLEXPRESS.
UPDATE: Ok got an update from the winhost forum. I shouldn't be using the server_name/Instance_name anyway so I am at a lost.
So my project was using the default database (SQL Express) even though I didn’t have a connection string pointing to it. The EF uses convention to attach the default database if you don’t specify by using a constructor like this:
public class BloggingContext : DbContext
{
public BloggingContext()
: base("BloggingDatabase")
{
}
}
Then this:
<configuration>
<connectionStrings>
<add name="BloggingCompactDatabase"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=Blogging.sdf"/>
</connectionStrings>
</configuration>
I found it all here…
http://msdn.microsoft.com/en-us/data/jj592674.aspx.

Cannot connect to Database with Entity Framework

Every time I have a fresh install of Windows, Visual Studio and Sql Server I always have problems getting an application to connect to the local instance of Sql Server.
I am trying to start an application using Entity Framework code first. Here is my connection string.
<add name="KCSoccerDataContext"
connectionString="Data Source=.\MSSQLSERVER;Initial Catalog=KCSoccer;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
I am able to connect to the database instance using Sql Server Management Studio, but can't seem to connect using this connection string. I am assuming the problem has to do with how the database instance is configured.
The error I'm getting is
{"The provider did not return a ProviderManifestToken string."}
I'm not exactly sure what this means or where to go from here.
Any help anyone could give on this would be awesome.
Thanks
Use just this connection string:
<add name="KCSoccerDataContext"
connectionString="Data Source=.;Initial Catalog=KCSoccer;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
MSSQLSERVER is name of default instance. It is not used in connection strings. It even doesn't work from Management studio if you try using it. Only names of custom instances must by used.
You probably didn't enable remote connections for SQL Server since it's a fresh installation. Follow first solution here or here.

EntityFrame work connection string issue

I am working on a Silverlight application. during development I have been working with a copy of our production database on my local machine.
When setting up the project I created a model of the local database, and then created a domain service of that model to interact with the data. This is all working well. Now I need to test my product against the live server, but I can not seem to figure out the connection string.
Currently the connection string looks like this.
<add name="UserDataEntities" connectionString="metadata=res://*/Models.UserDataModel.csdl|res://*/Models.UserDataModel.ssdl|res://*/Models.UserDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=RTRP20112_NATP_UserData;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
The only differance between the database I have been using locally, and the production database is its location. I have attempted to simple replace the data source portion of the connection string with the correct address, and login info but that does not work. I have also duplicated the connection string used by another application to connect to this database, but that does not work since it is missing all the metadata junk(i think). I am not sure how to proceed.
The connection string used by other programs to connect to the server is
<add name="UserDatabase" connectionString="Data Source=*.*.*.*,*;Network Library=DBMSSOCN;Initial Catalog=RTRP20112_NATP_UserData;User ID=*;Password=*;" providerName="System.Data.SqlClient"/>
I have tried a few variations of the two as a connection string, most recently I am using.
<add name="UserDataEntities" connectionString="metadata=res://*/Models.UserDataModel.csdl|res://*/Models.UserDataModel.ssdl|res://*/Models.UserDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=*.*.*.*,*;Network Library=DBMSSOCN;Initial Catalog=RTRP20112_NATP_UserData;User ID=*;Password=*;App=EntityFramework"" providerName="System.Data.EntityClient" />
The most recent error is: Load Error
System.ServiceModel.DomainServices.Client.DomainOperationException:Load operation failed for query 'GetUsers'. The undelying provider failed on Open.
Have you configured/created ASPNETDB ?

MVC3 changing default connection string error

I created an MVC3 Internet application and changed the default connection string to work with
.\SQLSERVER instead of the default .\SQLEXPRESS and get following error :
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
I tried creating an empty MVC3 application as well, which has no connection string by default and added mine with the same result.
The SQLSERVER instance is running and I can connect to it from management studio using windows authentication. Any connection string that I use gives same error, I cannot figure it out:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="data source=.\SQLSERVER;" providerName="System.Data.SqlClient" />
</connectionStrings>
To connect to your local default instance (i.e. SQLSERVER service name), for Data Source=..., use one of the following:
127.0.0.1
localhost
(local)
.
EDIT
Your connection string should resemble this:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Checkout the different connection strings you can use with SQL Server. For example if you have a SQL Server installed on your local machine you could use the following connection string:
Data Source=127.0.0.1;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Obviously you will need to adjust the name of the database, the username and password to connect to.
If you want to use Windows Authentication to connect to the server:
Data Source=127.0.0.1;Initial Catalog=myDataBase;Integrated Security=SSPI;
Your connection string needs to look something like this:
<add name="NameOfConnection" connectionString="Data Source=localhost;Initial Catalog=OMSCING;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />
Where you can replace localhost with a hostname or leave it like that if the server's local.
Perhaps I had to mention that I was using EF with code first approach, but the problems was due to the name of the connection string which should be the same name as your Context which you derive from DbContext, e.g.:
<connectionStrings>
<remove name="ApplicationServices"/>
<add name="MyContext"
connectionString="data source=localhost; Initial Catalog=CoolCars;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
as well make sure you remove the default machine.config connection string which is for .\SQLEXPRESS

Resources