Gerald Torres
Gerald Torres

Reputation: 403

Connecting a client-Installed C# application to an exported SQL Server database

I'm very new to creating installers. This application needs to connect to a SQL Server database for it to be used. The tables of that database has rows which are needed by the application, so just reconstructing the database structure is not what is needed.

Can I just export the database as a .mdf file and connect from there?

Or do I need to install SQL Server Express on those computer as a prerequisite and attach the database from there? If so, how do I attach it on installation?

Or are there other ways to include a SQL Server database to an installer and be used by the installed app?

Upvotes: 0

Views: 1183

Answers (1)

Steve
Steve

Reputation: 216290

On the target machines you need to install SqlServer (express or standard).
You don't need any management application like Sql Server Management Studio.
Then you can attach your MDF to the database engine via connection string as this one

Server=.\SQLExpress;AttachDbFilename=c:\yourfolder\yourfile.mdf;Database=yourdatabase; Trusted_Connection=Yes;

The installation of SqlServer is not an easy task to do via custom installers.
If your application is not intended to be used outside of the local machine you could use LocalDB.
The install of this flavor of sqlserver is more easy.

Other alternative is:
Extract a sql script that rebuilds your database. (and provide a method to execute the script)
Export the data needed and execute a bulk insert to reinsert the data in the rebuilded database

Upvotes: 1

Related Questions