Cannot connect to attached SQL database - c#

I'm using linq to sql with Microsoft SQL Server Database File (SqlClient) as a data source.
The db file was created by Visual Studio 2010 while adding the connection (Database Explorer -> Add Connection).
Using this method I was unable to create a db with SQL auth, so I'm using Windows Authentication.
EDIT: Note that it's not a connection to a database, it's a connection to a database file.
1) Is it possible to create a database file with sql authentication? (I assume it is the root of the problem).
2) Connection works fine on my computer under different acocunts. However, running the release on other machine results in an exception being thrown.
Exception details:
System.Data.SqlClient.SqlException (0x80131904): 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)
I've tried to set the db path manually, but I guess it's not the issue.
What am I doing wrong?
EDIT2: the connection string I'm using:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\OperatorResults.mdf;Integrated Security=True;Connect Timeout=10;User Instance=True"

First of all, how was SQL Server installed? With Windows Authentication only or Mixed Mode authentication. If it was just Windows Authentication that explains why you were unable to use SQL Server authentication. If mixed mode authentication is enabled then you need an account that has sysadmin or CREATE DATABASE privileges.
As for your second question, did you modify connection string to match the connection configuration of another machine? If not, try doing that. From the exception above it seems that your connection string is connecting to a wrong server or the server instance is not running.

You want Microsoft SQL Server Express
It allows a file based connection.

Related

ASP.NET Core Web 3.1 - IIS Express can fetch data from localdb but local IIS can not

I have a very weird problem as stated above.
The code running is exactly the same, the only difference is the Debug profile used. I'm using Microsoft Visual Studio Community 2019 Version 16.6.3.
When using the IIS profile the code says I have no Media in my database.
Switching profile to IIS Express, now I have Media using the exakt same connection string as can be seen in Immediate Windows.
I first suspected that the ASP.NET Core Web 3.1 had a problem fetching the connection string from appsettings.json and that it was needed in web.config as well. Tried adding it but it did not make a difference. Checking context.Database.GetDbConnection().ConnectionString also shows the same for both profiles so this is not the case.
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=(localdb)\\mssqllocaldb;Database=<MYDB>;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>
If I look at the database via SSMS I can see my Media as expected.
The IIS Application Pool runs as LocalSystem. If I switch to a user that does not have access to the database like ApplicationPoolIdentity I get an exception as expected in Windows Event Viewer and the application does not start correctly.
Microsoft.Data.SqlClient.SqlException (0x80131904): 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: 50
Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
Using LocalSystem everything looks fine in Event Viewer but it says I have no Media or any other data.
How can two identical connection strings using the same code show different results?
Tried switching Application Pool Identity to my currently logged in user that I know has access via SSMS:
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: 50 - Local Database Runtime error occurred.
Error occurred during LocalDB instance startup: SQL Server process
failed to start. )
With this and the comment from #nilsK I fixed it like this:
Start by using Developer PowerShell or Developer Command Prompt for Visual Studio as Administrator and confirm that you only have one instance of LocalDb.
Type sqllocaldb info to see your LocalDb instances.
I then followed this article and had Application Pool Identity still set to my currently logged in user, credit: https://stackoverflow.com/a/38294458/3850405
https://learn.microsoft.com/en-us/archive/blogs/sqlexpress/using-localdb-with-full-iis-part-1-user-profile
In there I found that it is not enough to have Load User Profile set to true for your Application Pool, you also need to set setProfileEnvironment to true in applicationHost.config normally located at C:\Windows\System32\inetsrv\config. With this configuration it worked:

How to Attach a database file with application so that it can work on a client pc in c#?

Can anyone tell me which database type should use in C# programming to store some data locally? I have used SQL Server database file and built a simple application but when I put this to another PC, it says
Could not connect to SQL Server Client
System.Data.SqlClient.SqlException (0x80131904): 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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
You could use a JSON file or sqlite, both of them do not requiere any installation on the client machine.
You have to install SQL Server in the other PC to run it local.
Another option is use some portable database, like sqllite, or access.
Or, try to use database server and access your database remotly.

permission and timeout issues connecting to local northwind database in C# entity data model

i am trying to connect to the northwind database in a C# project (entity data model). i am running on my own machine as admin, and have permissions both as admin and as user Gary\PC-Gary
In the Connection Properties dialog box , when i locate the northwind datbase, and double click it
i get the following error message:
Northwind
you don't have permission to open this file.
Contact the file owner or an administrator to obtain permission
i detach the database from SQL Server (on the understanding that C# 2010 will not allow a user instance to connect to a database that is currently in use)
I seem to be making progress, because now, when I browse to the database file, i seem to get past the connection issues, and CAN pull up the filename.
HOWEVER, when i attempt to test the connection (or create the EDMX model), i get the following error message. i am using windows authentication.
a network-related or instance-specific error occured 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)
i have verified that the instance is running in SQL Server Configuration manager and in SSMS. does sql server have to be configured to allow remote connections to access a database on your own machine?? how would this be done?
Found a suggestion here by Peja Tao. Maybe that will help.
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/8cdc71eb-6929-4ae8-a5a8-c1f461bd61b4/provider-sql-network-interfaces-error-26-error-locating-serverinstance-specified

Unable to connect to sql server 2008 with c#

I have installed SQL Server on one PC and I have created a c# program on another computer to connect to the server. However, when I run my program, I get an exception saying that login failed for user 'testing'.
This is the code used to connect:
SqlConnection con = new SqlConnection("Data Source=IP,PORT;Network Library=DBMSSOCN;Initial Catalog=Stockinfo;uid=testing;pwd=testing");
I have created a login in the server with userid and password as testing. As for the server authentication, it has been set to SQL Server and Windows authentication. I have also created an exception for firewall. What have I done wrong?
Thanks in advance for your help!
EDIT 1: I am able to access the server with SQL Server authentication using the same userid and password on the computer running the server.
Do you have Sql Server Management Studio. If yes, then you can open it and right click the instance in question, go to Properties -> Connections -> Check "Allow remote connections to this server".
You might also have to enable TCP/IP Protocol to be enabled thru Configuration Manager.
Does it return 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)
You can check this link: enable remote connections in SQL Server 2008
Another reason is to check another computer's firewall if port sqlbrowser (or port 1433 is open) was added on its firewall.
UPDATE 1
user testing simply doesn't exist or you have supplied incorrect password
Login failed for user 'testing'.
That usually means that the user doesn't have access to that database.

Network related error while deploying a window application & used database to another pc

My window application works fine on my computer but when I install it on another computer a network-related error occurred...
I'm using sql server 2005, I have store the backup of the database, when i run it, it flags an error.
I'm using visual studio 10, C#,sql sever 2005
Error is..
"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)""
When this happens it means that the program cannot connect to SQL Server with information you provided.
If for example you are trying to connect my-server\sqlexpress, usually you have one of this issues:
There is no computer named my-server in your network!
SQL Server is not installed on my-server.
Your client computer cannot reach my-server from the network. You can check this using ping command.
The installed SQL Server instance name on my-server is not sqlexpress (case-insensitive).
You have not enabled TCP/IP protocol for your installation instance of SQL Server in "Sql Server Configuration Manager".
You have not restarted SQL Server service after enabling TCP/IP.
Your SQL Server instance is not default instance, and SQL Server Browser service is not running on the server.
Server's firewall is preventing the client to access the server. Try turning off the firewall on the server (only to test this case).
Consider this a checklist, and start from the beginning. Check them one by one. And I hope that you find the problem.

Categories