Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Coders,

I'm creating a executable file however to requires sql local database to function. I'm having difficulty to figure out connection string code, once I deploy it to another computer that DO NOT have sql server express. Could you help me ?

I guess this would be easy using Microsoft Access, but MS SQL Server is the require database for school.

What I have tried:

This is my current code but not working after searching to multiple sites. I also have tried to make a uninue database name still does not work.

Connection.ConnectionString = "Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\test_sqlDb.mdf; Database=test_sqlDb;Trusted_Connection=Yes;"

This is the ERROR:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Database 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\test_sqlDb.mdf' already exists. Choose a different database name.

Cannot attach the file 'C:\Users\user\documents\visual studio 2015\Projects\AccessDb_Test\AccessDb_Test\bin\Debug\test_sqlDb.mdf' as database 'test_sqlDb'.
Posted
Updated 15-Dec-19 0:52am

Two problems:
1) You are Attaching a DB - that is a test function which is only implemented in Express versions of SQL Server for development purposes. You cannot use ATTACH in production versions of SQL Server as it uses far, far too many resources.
2) Look at the error message: the file you are trying to attach already has a DB of the same name in the SQL Data folder - it can't be attached as it would overwrite the existing data and SQL doesn't want to do that.
 
Share this answer
 
In addition to Griff's answer, you can test if LocalDB is installed from the command prompt:
SqlLocalDb info
More information here: https://www.sqlshack.com/how-to-connect-and-use-microsoft-sql-server-express-localdb/[^]
 
Share this answer
 
Quote:
once I deploy it to another computer that DO NOT have sql server express.

If there's no SQL server instance installed, you can't run your programme, no matter what you write in the connection string.

You've got two options to resolve your issue:
1) create one SQL server in a LAN/WAN and enable remote connections to it - strongly recommended!
or
2) install SQL server instance on each computer you want deploy your programme - not recommended at all! due to several reasons, but the most important is: you'll be in trouble with data synchronization.


So, follow this link: Configure the remote access Server Configuration Option - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900