none
how to attach sql database file to creating .exe setup in c#

    Question

  • I create a Setup file and put data folder, in data folder i put database file , but the problem is i have to mannualy attach database file in sql server 2005. I have to automatically attach database file when installing setup file.
    • Moved by edhickey Tuesday, September 14, 2010 4:17 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Tuesday, September 14, 2010 9:21 AM

Answers

  • Hi bhagvad,

    Welcome to MSDN Forums!

     

    We can implement auto attach database just use the connection string following.

     

    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 the sql server management studio to find it.

     

    In addition, you can find the connection in the app.config file through the solution explorer in vs2010. And you also can find this file under your project. With these information, you can find it and modify it.

     

    If there’s anything unclear, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Thursday, September 16, 2010 4:22 AM

All replies

  • Hi bhagvad,

    Welcome to MSDN Forums!

     

    We can implement auto attach database just use the connection string following.

     

    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 the sql server management studio to find it.

     

    In addition, you can find the connection in the app.config file through the solution explorer in vs2010. And you also can find this file under your project. With these information, you can find it and modify it.

     

    If there’s anything unclear, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Thursday, September 16, 2010 4:22 AM
  • Hi there,

    I'm also trying to attach a database file (.mdf) during a with VS2010 created setup using prerequisites.

    I have some questions about your reply:

    1) Where can I find this Connection String? Do I have to add these lines in the product.xml or package.xml of a SQLServerExpress prerequisite?

    2) Where can I find the app.config file? It doesn't seem to be part of my project as I can see it neither in the solution explorer nor under my project (project path).

     

    Thank you for your help!

    Best regards,

    Manuel

    Tuesday, November 16, 2010 2:01 PM
  • I'm relatively new to this discssion group and not totally sure if this is what you want:

    http://www.connectionstrings.com/

    Hopefully, though, it helps.

    Ryan--


    Ryan Shuell
    Tuesday, November 16, 2010 3:16 PM
  • Hi Manuel Wedel,

    Welcome to MSDN Forums!

     

    If you have any question you can create a new thread, this will help you to get more help. There’re more people will read the most recent thread and try to answer it, this will help you solve your question quicker and better.

     

    And if you want us to read this thread you can use a link in your post. This thread is answered and closed so there’ll be a little community members will found your question because of most members only search the recent thread and answer it when the thread has been not closed.

     

    Best wishes,

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Wednesday, November 17, 2010 3:07 AM
  • hi guys,

    i got ur question and here is the solution

     

    go to solution explorer then right click on it and go to add new item

    in this selection,select service database in it

    it will be a .mdf file that you want to attach with your database

    treat this database file normally as other database files

    double click on it and you will found it in server explorer

    then add some required tables in it

    while making .exe files go to add files and add this database and then rebuild

    now you can run it on any pc.

    * some point to be remember

    1. add sql server as prerequisite 

    2.change your connection string to datasource=|DataDirectory|\filename.mdf

    this is all about your solution

    feel free to ask any question

    best regards

    sunil dhanerwal


    sunil dhanerwal
    Wednesday, April 27, 2011 6:09 PM
  • Kindly place a sql script to attach the database to the sql server in the setup and write a java script to execute the sql query and point the java script in the postbuildevent in the setup

    Regards,

    Narendran Ponpandiyan

    Thursday, April 28, 2011 5:20 AM
  • i have a question to ask:-

    in our visual studio 2008,it is easy to make a column primary key in sql but how we can make a collumn unique key.because i want to allow null property in it which can't be achieved in primary key.

    thanks in advance

    regards

    aunil dhanerwal

     


    sunil dhanerwal
    Friday, May 06, 2011 8:17 AM
  • Hi bhagvad,

    Welcome to MSDN Forums!

     

    We can implement auto attach database just use the connection string following.

     

    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 the sql server management studio to find it.

     

    In addition, you can find the connection in the app.config file through the solution explorer in vs2010. And you also can find this file under your project. With these information, you can find it and modify it.

     

    If there’s anything unclear, please feel free to let me know.

     

    Best wishes,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    when i try this i have an error from the computer. in my computer just microsoft server 2005 installed. and i need to setup my project. 

    <add name="TelekomEthernetApp.Properties.Settings.TelekomEthernetConnectionString" 

           connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\DB\TelekomEthernet.mdf;Initial Catalog=test;Integrated security=true" providerName="System.Data.SqlClient"/>

     

     

    what is the problem?

    http://img31.imageshack.us/img31/2305/asdet.png

    Monday, August 22, 2011 11:36 PM
  • Hi Mike,

    I found this connection string to work better...

    DataSource=.;AttachDbFilename=|DataDirectory|\DB\DataBaseName.mdf;IntegratedSecurity=True;Connect Timeout=30;User Instance=True

    Thanks


    SquireDude
    Wednesday, August 31, 2011 4:07 PM
  • Hi 

    I am also facing the same problem.I have an application with the sql serever and a crystal report with it.I have a login in the system with the database.

    Whenever I install it on the clients pc or any other pc, I get an error that the as same as Gardiann

    I have followed all the steps mentioned in this question thread.

    Please let me know where I m going wrong

    Thanks

    Friday, May 24, 2013 12:13 PM
  • Hello
    I had the same problem Babbt If you find a solution tell us about it.
    Thank you

    Tuesday, June 04, 2013 9:34 AM
  • Hello
    I had the same problem Babbt If you find a solution tell us about it.
    Thank you
    Tuesday, June 04, 2013 1:46 PM
  • But it creates problem and generates a SQL exception when I try to run this setup in another desktop.

    So what additional things I need to implement so that my setup with a database run on another desktop without any problem?? 
    Help!!!

    Thursday, June 20, 2013 7:34 AM
  • Hi,

    Maybe I have the solution for you in C#. Attach database to the SQL server:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace AttachDatabase
    {
        class Program
        {
            static void Main(string[] args)
            {
               string Argument = string.Empty;
    
               if (args.Length > 0)
               {
                   for (int i = 0; i < args.Length; i++ )
                       Argument += args[i]+" ";
    
                   Argument = Argument.Trim();
               }
    
                if (string.IsNullOrEmpty(Argument))
                {
                    Console.WriteLine("Geen filenaam met path ingetyped.");
                    Console.ReadLine();
    
                }
                else
                {
                    try
                    {
                        string connectString =
                            @"Server=.\sqlexpress;" +
                            "Integrated Security=true";
                        SqlConnectionStringBuilder builder =
                            new SqlConnectionStringBuilder(connectString);
                        Console.WriteLine("Origineel: " + builder.ConnectionString);
                        Console.WriteLine("AttachDBFileName={0}", builder.AttachDBFilename);
                        
                        
                        string FileNaam = Path.GetFileName(Argument);
                        Console.WriteLine("File = {0}", FileNaam);
                        string[] line = FileNaam.Split('.');
                        // Argument = line[0];
    
                        // Console.WriteLine("Kort : {0}", Argument);
    
                        //  Zet de bestandsnaam in de data
                        
    
                        builder.AttachDBFilename = Argument;
                        Console.WriteLine("Modified: " + builder.ConnectionString);
    
                        builder["database"] = line[0];
    
                        using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                        {
                            connection.Open();
                            // Now use the open connection.
                            Console.WriteLine("Database = " + connection.Database);
                        }
                        // Console.WriteLine("Druk op enter om het programma te beeindigen.");
                        // Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.ReadLine();
                    }
                }
            }
        }
    }
    

    Run this program when running setup

    Regards Rinaldo


    Full of ideas but don&#180;t know how.

    Thursday, June 20, 2013 6:51 PM
  • does this mdf file supports stored procedures and triggers??
    Tuesday, September 10, 2013 2:33 AM