0

Is it possible to copy my database files i.e .mdf files to a destination folder which is my SQL Server folder during setup of my windows C# application?

Also, how do I attach those filed to the database of sql server automatically in setup?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2274463
  • 9
  • 1
  • 7

1 Answers1

0

You can use an attached database for your application. This will help you in creating setup for that.

  1. Add a folder named ”DB” to the project, and copy the database file into it after you detach it from your SQL Server.

  2. Change your connection string like this:

    <connectionStrings>
        <add name="WindowsFormsApplication1.Properties.Settings.BabakConnectionString"
            connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\DB\Babak.mdf;
            Initial Catalog=test;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    
  3. After these, the database file will auto attach to the SQL Server when you run your application, and the database name is “test”. you can open SQL Server Management Studio to find it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • Sir but the problem is when i will be installing this application on some remote computer the connection string will be different and when i create a database project the connection string for my computer is automatically generated and it is non-editable – user2274463 Apr 12 '13 at 13:33
  • that is why i am suggesting you use attached database, check the updated code – Mohammad Arshad Alam Apr 12 '13 at 13:38