Setting up EntityFramework SQL Server on Windows Server 2012 r2 - c#

This is the first time I've ever had to use a Windows server for publishing so forgive my ignorance.
I have a dedicated server with Windows Server 2012 r2 installed. I have setup the FTP publishing in order to publish the site via Visual Studio. The site works fine unless I try and use a feature that requires a database connection.
The project I am developing uses EntityFramework to handle the SQL database.
And I receive the following error:
Exception Details: System.ComponentModel.Win32Exception: The system
cannot find the file specified
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.)]
Connection strings
CentralDb
metadata=res://*/CentralDb.csdl|res://*/CentralDb.ssdl|res://*/CentralDb.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\centraldb.mdf;integrated
DefaultConnection
Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-BetCentral-20161230033657.mdf;Initial Catalog=aspnet-BetCentral-20161230033657;Integrated Security=True
LocalSqlServer
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I've done a bit of Googling for this error and I have found multiple answers that are very varied and have not helped me correct my problem.

Related

ASP.NET Core not connecting to database on Windows Server 2012

I published my ASP.NET Core application to a Windows Server 2012 environment. I made a new website, pointed it to the folder and it was running fine. Now I am trying to make the application connect to a database. I installed SQL Server Express 2016 and edit my project so it retrieves data from the database. put my 'KSP_Backend.mdf' file to my server and I attached the file in SQL Server Management Studio. The database is completely visible, so it exists.
Now, when I try to run my project in my published project folder 'dotnet KSP_Backend.dll', it gives me the following error:
Unhandled Exception: System.Data.SqlClient.SqlException:
A network-related or instance-specific error occurred while establishing 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. Thespecified LocalDB instance does not exist.)
This is the connection string in the 'appsettings.json' file I used:
"Server=<My server's name>\\KSPSERVER;Database=KSP_Backend;Trusted_Connection=True;MultipleActiveResultSets=true"
I'm just not sure anymore if the connection string caused my error. My firewall is off and LocalDB is up and running.
I had the same issue when I didn't have 'Remote connection' enabled on the DataBase's Server.
In SQL Management Studio, right click the Server name, then Properties, go to tab Connections, make sure that Allow remote connections is enabled.
Another thing could be that the SQL Server TCP/IP protocol is not enabled.
In SQL Server Configuration Manager, click on SQL Server Network Configuration in the left pane and right-click TCP/IP and select Enable.
Finally, you might need to configure SQL Server Express to listen to a static port. You can find a good reference for this and the previous points here: SQL Express Remote Connections.

SQL connection error using visual studio code mssql extension on macOS

I am currently developing an ASP.NET app using Visual Studio Code on mac.
I am trying to use "mssql extension" to get connection with a SQL Server database.
I followed https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-develop-use-vscode step by step. However, I always get an error message like below:
mssql: Failed to connect: System.Data.SqlClient.SqlException: 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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
"error message screenshot"
Thanks for your help in advance.
If this is a SQL Server 2008 R2 instance, you need to upgrade to SP3. See https://github.com/Microsoft/vscode-mssql/issues/666 for details - basically .Net Core doesn't support older versions due to a vulnerability that is fixed in SP3, and the MSSQL extension uses the .Net Core stack for connectivity.

SQL database failing to initialise on second PC with message 'The underlying provider failed to open'

I have a VSExpress 2013 solution (with 3 projects) with Entity Framework (ADO.NET) accessing a SQL Server database. I am able to publish my project to the original source PC, and it works as expected, but attempting to install on a second PC via CD fails. The installation is failing to instantiate or create the necessary database files on the local SQL Server (localDB), although SQL Server Express (LocalDB) installs correctly.
I tried adding the line:
Database.SetInitializer<PMMEntities>(new CreateDatabaseIfNotExists<PMMEntities>());
to MainWindow() on start-up to initialise the database. However, I still get the following error on running the application on the other PC:
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 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)
The connection string in app.config is (note that I am not trying to access a shared database on a network, and each installation on another PC must create and run its own local DB):
<connectionStrings>
<add name="PMMEntities"
connectionString="metadata=res://*/PMMData.csdl|res://*/PMMData.ssdl|res://*/PMMData.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=PMM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
I am using ClickOnce settings. What do I need to do to achieve effective installation on other PCs so that they will instantiate a new database on first execution?
Sounds like SQL Express is not installed on the second PC, it's being installed with a different instance name, or the installation is failing during the ClickOnce deployment.

how to access sql server 2008 express database using clickonce

I have created a desktop application with visual studio 2012 and Sql Server 2008 R2 and want to deploy the application on client machine.I am using clickonce technique to deploy the application.How should I deploy the database on client machine or what connection string should be mentioned during publishing the application.By default the connection string is :
Data Source=.;AttachDbFilename=|DataDirectory|\DatabaseName.mdf;Integrated Security=True
When I create a clickonce application 3 files get created
setup.exe
ABC.Application
Folder named Application files
When I click on setup.exe necessary setup get installed but when i try to access the database from installed application it throws error :
*Unhandled exception has occured in your application.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) *

SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified on LocalDB

I have written a c# wpf application that talks to a SQL Server 2012 LocalDB on my machine.
To make the app accessible on other peoples machine's I have:
Included the database file in a zip for the user to extract to the programs directory.
Written SQL scripts to attach the database to the local machine's LocalDB instance.
The application works perfectly fine on my machine, however after installing on someone else's computer (after the .NET and SQL Server 2012 LocalDB prerequisites have been installed), it keeps filling up the error logs with the following message:
Exception: 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).
Now there are a lot of other references to the same error message, but I cannot find any that relate to LocalDB or what to try with LocalDB.
Keeping in mind the application is designed for the common 'end-user', so I can't have them following numerous instructions to use SQL Server Management Studio or configuration manager.
The attach SQL script is as follows in case you need to know:
CREATE DATABASE BaseDB
ON (FILENAME = 'C:\MyApp\Base\MyDB.mdf'),
(FILENAME = 'C:\MyApp\Base\MyDB.ldf')
FOR ATTACH;
Can anyone suggest why this wouldn't be working?
To solve this problem:
Install this update to Microsoft Net Framework 4.0: KB2544514
Start: `SqlLocalDB [create|c] instance-name [-s]`
For example:`SqlLocalDB.exe create MSSQLLocalDB -s`

Categories