I am developing a Windows program, using C# and VS 2022. This program is linked to a .mdf
database file, and everything works fine in development.
The next step was to create an installer for it. I used a setup project template, where I added the .mdf
, the executable, leaving the rest up to the wizard. As prerequisites, I chose .NET Framework 4.7.2 and SQL Server 2019 Express LocalDB. Also, I created a file, CustomInstaller.cs
, with the following code:
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.IO;
[RunInstaller(true)]
public class CustomInstaller : Installer
{
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
AttachDatabase();
}
private void AttachDatabase()
{
string targetDir = Context.Parameters["targetdir"];
string dbFilePath = Path.Combine(targetDir, "Colibri.mdf");
//string connectionString = @"Server=(localdb)MSSQLLocalDB;Integrated Security=True;";
string connectionString = @"Data Source = (LocalDB)MSSQLLocalDB; Integrated Security = True; Connect Timeout = 30;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = $"CREATE DATABASE Colibri ON (FILENAME = '{dbFilePath}') FOR ATTACH";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
}
}
}
When I first tested the installer, everything went fine, until I got the error message:
Unable to connect to SQL Server instance
then everything rolled back.
The problem is that, now, I cannot use the database functions from inside Visual Studio:
-
I cannot access the
.mdf
file which I use for development. I cannot open it from the Server Explorer, because I get the following message:The attempt to attach to the database failed with the following information:
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. -
I cannot connect to the server, from the SQL Server Object Explorer window, because I get the error message that the login failed for my user (error 18456), although I never tinkered with any login details.
-
Reinstalled the entire Data Storage and Processing Toolset from the VS installer. Nothing.
-
Installed SQL Server 2019 from Microsoft website. Nothing. Uninstalled it after.
Now, however, if I run the setup again, I get the following error message:
Error 1001. A network-related or instance-specific error occurred while establishing a connection to the SQL server (…). Error 50: Local Database Runtime error occurred…
If I change the name of the database from the above file, I get the following error message:
<new file name>
could not be found
So, there is something controlling the server, but not me.
What did I do wrong? How can I fix this?