Problem accessing database for Entity Framework - sql-server

I've created a windows forms application that would use a client server model for accessing database. I created a connection for Entity Framework Model and it works fine with the following connection string:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
AttachDbFilename=E:\Databases\RollMeasurements.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
I wonder if this connection string would still work when i deploy it on client environment which has a central db server and more than one instance of my app would be accessing it from client machines.
I tried changing the connection string to this:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
Initial Catalog=RollMeasurements;
User ID=sa;
Password=sapassword;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
But now when I run my app it gives me this exception:
Failed to generate a user instance of SQL Server. Only an integrated
connection can generate a user instance. The connection will be
closed.
Please help me with this. Thanks!

I think you have to remove User Instance=True (or set it to False explicitely). User Instances are local copies of a SQL Express database and not suited for work in a multiuser environment with a central DB.

I think if you want to access to a central server, you need to change the Data Source=.\SQLEXPRESS; line to a central sql server instance.
english is not my mother language so maybe dont understand correctly the question.
Cheers

Related

SAS connection to SQL Server DB using Windows Authentication

Can anyone give me the libname statement for making SAS to SQL Server connection using windows authentication or is it even possible without entering userid and password?
Thanks
You will want to use a connection string that has Trusted_Connection=True; How that property is communicated to the engine is dependent on the engine.
A great resource for all things connection strings is https://www.connectionstrings.com/sql-server/

Issue with website deployed to IIS and connecting to SQL Server 2012, getting network not found error

I have deployed a website to IIS and it is trying to access the database via the connection string.
My SQL Server and connection string is correct as I have tested it in the following ways:
My development environment streams data correctly
Connection to SQL Server is good as I could see from SQL Server Management Studio
Other pages of the website that are deployed are correct and only those pages that stream data is failing.
The problem is that it is only through the IIS website we have this error and when I run it from Visual Studio it is all right.
Here is the stack code I get:
Stack trace error
My connection string:
<add name="cnnSQLDB"
connectionString="server=**********;database=*********;Integrated Security=True;User ID=******;Password=******;"
providerName="System.Data.SqlClient"/>
Make sure that port 1433 is open on your firewall. That is the port SQL Server uses.
I was able to solve this issue based on hints provided here. I had to do the following changes:
Update the SQL connection string and set "Integrated security=false". In that way it will use the login credentials provided in the SQL connection string.
Create a new user as provided in the SQL connection string and GRANT the necessary accesses.
When I copied the databases from the production environment to a new environment the GRANT permissions for the user mentioned in the connection string was not updated.
But by checking the port status via the command 'sp_readerrorlog' gave me clues and led me to a solution. Some other links that helped me were: this and this

ODBC DSN Connection Error - SQL Server 2014 Express Classic ASP

Trying to connect a classic asp page to sql server express 2014 using a dsn.
I've created a new database (in sql server management studio) called warehouse, with a table called users. I've added a new login called user1 with a password and mapped it to the warehouse database
I've created an ODBC System DSN on the same machine, using sql server native client 11 driver called SQLServer2 with integrated windows authentication, and a default database warehouse. Testing the connection at this point works fine!
On the ASP page I'm using:-
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=SQLServer2;uid=user1;pwd=xyz;"
Error:- Microsoft OLE DB Provider for ODBC Drivers error '80040e4d'
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open
database "warehouse" requested by the login. The login failed.
I'm pretty sure that its a permissions issue and that the connection string is OK otherwise but after hours and hours of trying different strings, different users, I can't seem to get the damn thing to connect
If Anyone can throw any ideas my way I'd be very grateful. Thanks!
The way you are describing the problem the DSN uses integrated security so your credentials might be ignored and the database is called using the application pool identity.
If you have no specific reason why you need to use a DSN I would recommend using a connection string like the following:
objConn.Open "Provider=sqloledb;Server=yourdbserver;uid=user1;pwd=xyz;Database=warehouse"
A look into your SQL server log might give you a hint as to which user is actually failing to connect to your database.
I think you should have done something like this
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString "dsn=SQLServer2;uid=user1;pwd=xyz;"
objConn.open

SQL Server Availability Group Automatic Failover Not Working

I have an ASP.NET MVC web application using Entity Framework to connect a SQL Server 2012 configured with high availability. It is configured to automatically fail over to a secondary server and this works well when trying to connect with SQL Server Management Studio. I could confirm this, and read or write data to database.
Somehow this doesn't happen from the application and I got the following error:
The target database ('DbName') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online.
This is the connection string that I am using:
<add name="DataModel"
connectionString="Data Source=DBCL01;Initial Catalog=Database;User Id=user; Password=password;"
providerName="System.Data.SqlClient" />
Anyone knows how could I solve this issue?
The problem is that your application is not being directed to the primary (writable) server, but is redirected to read-only secondary replica.
I had a similar problem, my problem was that the connection string was lacking ApplicationIntent=ReadWrite and that the availability listener wasn't being resolved good by the DNS.
Also check this AlwaysOn Availability Groups Connectivity Cheat Sheet since bad setting of connectivity options for availability group can be a cause for connectivity errors and denial for connection to primary server.

Converting Entity Framework connection string to SQL Server Express

My project has an Entity Framework connection string but I want to connect to a SQL Server Express database, so I think I have to change the connection string to SQL Server Express - but how ?
I want to change below connection string. Is it also enough connect database just changing connection string for same SQL Server mdf file ?
<add name="MyEntities"
connectionString="metadata=res://*/Model.MyEntities.csdl|res://*/Model.MyEntities.ssdl|res://*/Model.TravldbEntities.msl;
provider=System.Data.SqlClient;
provider connection string="Data Source=sandiego;
Initial Catalog=mydatabse;Persist Security Info=True;
User ID=user;Password='password';MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
What have you tried? I'm not familiar with EF, but why not just try something similar to how a normal C# app would connect:
<add name="MyEntities"
connectionString="provider=System.Data.SqlClient;
Data Source=sandiego; -- maybe needs to be sandiego\SQLEXPRESS?
User ID=user;
Password=password;">
I would only specify the MARS attribute if you know for sure you need it.
Instead of this:
Data Source=sandiego
Use this:
Data Source=SomeMachineNameOrIP\SQLExpress
Here's another similar answer.

Resources