MVC3 changing default connection string error - sql-server

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

Related

Changing Connection String to SQL Server Express has no effect

Initially, my project (running on localhost with SQL Server Express) had a connection string to the database Test1:
<connectionStrings>
<add name="TestContext"
connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; Initial Catalog=Test1; User Instance=true"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
Now, I want the same project uses a different database: Test2.
So I've changed the "Initial Catalog" in the connection string to Test2.
But, to my surprise, when I execute the application, the Test1 database is still the one to be used.
What am I doing wrong?
I've already try to stop/start the IIS and delete the "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files" folder with no success.

Entity Framework using an incorrect connection string

I try to connect to my database(which is recently moved to another server) with EF.
Server name is :SERVER-PC
Version of MSSQL : Microsoft SQL Server 2014 - 12.0.2000.8
The instance name when I get it using xp_regread sp: MSSQL12.MSSQLSERVER
I want to ad both instance name , and server name in connection string.
this is my connectionstring which I got help from this link
<add name="DefaultConnection" connectionString="data source= SERVER-PC\MSSQL12.MSSQLSERVER; Initial Catalog=FoodDB;Integrated Security=True" providerName="System.Data.SqlClient" />
but when I run the project , this error happens:
error : {"The provider did not return a ProviderManifestToken
string."}
An error occurred while getting provider information from the
database. This can be caused by Entity Framework using an incorrect
connection string. Check the inner exceptions for details and ensure
that the connection string is correct.
Could anyone please give me a help?
Note: I want to use both server name and Instance name (intentionally) in connection string. (assume the instance is not default)
The EF unable to determine the SQL Server store version:
-- Get Server name
SELECT ##servername
-- Get service name
SELECT ##servicename
MSSQL12.MSSQLSERVER = instance Id and not name!
<add name="DefaultConnection" connectionString="data source= .\; Initial Catalog=FoodDB;Integrated Security=True" providerName="System.Data.SqlClient" />
or
<add name="DefaultConnection" connectionString="data source= SERVER-PC\; Initial Catalog=FoodDB;Integrated Security=True" providerName="System.Data.SqlClient" />
Note: To connect to the default instance you don't specify the instance name, only the server name.
MSSQLSERVER = default name

MVC4 Is my Connection String to SQL Server 2014 correct

I'm using Visual Studio 2012 for Web and I'm coding a MVC application. By default it uses a localDB but I want it on a SQL Server. I've installed SQL Server 2014 and my servername is C0208\SQLEXPRESS.
C0208 is my computer name
Named Instance: SQLEXPRESS
InstanceID: SQLEXPRESS
This is my connectionstring to the localdb
<add name="IssueContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=RecreationalServicesTicketingSystem;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\RecreationalServicesTicketingSystem.mdf" providerName="System.Data.SqlClient" />
I tried using this connection string and I don't know if it's correct or not
<add name="IssueContext" providerName="System.Data.SqlClient" connectionString="Data Source=C0208\SQLEXPRESS;User Id=C0208;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
This looks more of an Entity Framework issue rather than the SQL connection string. Just check if you have updated the connection string everywhere. (app.config).
To Enable data migration you will have to run the following command in the Package Manager Console.
enable-migrations
if it gives an error than try the following command first
add-migration projectName
For more details check this link

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.

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 ?

Resources