sql Connection .mdf Erorr

Ashen_Boy

New member
Joined
Sep 27, 2022
Messages
3
Programming Experience
Beginner
I'm coding project Using MS Visual Studio 2017 to enter data to sql Database. when i'm publish the project to another pc its can't connection to sql database show error like image below . the connection string =

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Car_db.mdf;Integrated Security=True");
Untitled.png
C#
 
The |DataDirectory| replacement pattern doesn't include a trailing slash, so you need to include one in your connection string:
C#:
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Car_db.mdf;Integrated Security=True");
That said, I don't think that things are even getting that far because I think that that would generate a different error message. There appears to be an issue with your LocalDB instance. Not something I've seen before but this may be helpful.
 
when i'm publish the project to another pc
Does your publishing include a setup program to install SQL Server Express on the other machine as well?
 
The |DataDirectory| replacement pattern doesn't include a trailing slash, so you need to include one in your connection string:
C#:
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Car_db.mdf;Integrated Security=True");
That said, I don't think that things are even getting that far because I think that that would generate a different error message. There appears to be an issue with your LocalDB instance. Not something I've seen before but this may be helpful.
same Error !!!
 
 
Click the Prerequisites button in Publish tab, there should be a SQL Server Express LocalDB option.
 

Latest posts

Back
Top Bottom