C# .NET - how can i attach the database mdf file to my application.

Asked By shoaib mansoori on 07-Jun-11 11:17 AM
HI all
i am creating the setup and deployment of my project but i can not attach my database file.
i am not attached the database with my setup file,but what i do, i does not know how to attached the database file with setup .
when i am running my application in VS2010 the code work properly i can do the insert update delete operation but at i run setup application so i cant do this
i m using sql server database for connection.
Ravi S replied to shoaib mansoori on 07-Jun-11 11:23 AM
HI

try this

Server mServer = new Server(".");
mServer.KillAllProcesses("attach1");
mServer.DetachDatabase("attach1", true);
File.Move("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\attach.mdf", "C:\\t\\attach.mdf");
File.Move("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\attach_log.ldf", "C:\\t\\attach_log.ldf");
Kirtan Patel replied to shoaib mansoori on 07-Jun-11 11:23 AM
you can create Setup Project and Include that Database File with EXe in Setup here is how you can create Setup Project .

Untitled document
Step 1:
In visual Studio >> File >> new >>Project >> Other Project Types >> Setup and deployment >>Setup Project .
 
Step 2 :
Add the exe(s) and other files in Application Folder of SetupProject
 
Step 3:
Now in Solution Explorer Right Click your Project >> Properties
Click Prerequisites Button and Check the packages you want to automatically installed if not present on system .
 
Step 4 :
Build the Project .
 
Step 5 :
you will get Setup :) thats it :)
 

Ravi S replied to shoaib mansoori on 07-Jun-11 11:25 AM
HI

here is full code

Server mServer = new Server(".");
mServer.KillAllProcesses("attach1");
mServer.DetachDatabase("attach1", true);
File.Move("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\attach.mdf", "C:\\t\\attach.mdf");
File.Move("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\attach_log.ldf", "C:\\t\\attach_log.ldf");
mServer.AttachDatabase("attach", new StringCollection { "C:\\t\\attach.mdf", "C:\\t\\attach_log.ldf" }, AttachOptions.None);



refer the link also
http://www.eggheadcafe.com/community/aspnet/2/10076647/how-can-i-attach-a-database-file-mdf-and-logfile-ldf-using-cnet-at-installation-time.aspx
http://stackoverflow.com/questions/5821864/add-mdf-file-to-c-project
Jitendra Faye replied to shoaib mansoori on 08-Jun-11 12:56 AM

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.

shoaib mansoori replied to Kirtan Patel on 08-Jun-11 06:49 AM
Thanks for reply sir
as per your answer i will do the same but they doesn't accept the mdf file its showing the error "the string must be a fully qualified URL or UNC path" so what i do
i want to attach my database file with my setup project so that i can perform insert update and delete operation without needing of sql server and vs2010.
i am creating this project for one commity thats why i can not provide the source code or vs2010 or sql server that why i am create setup project but after setup project i can not perfrom CRUD operation.

Kirtan Patel replied to shoaib mansoori on 08-Jun-11 06:52 AM
thats because you written database path in Connection String as absolute path write it as dymamic path ..
programmatic identify your current exe's directory and then wrote Connection String database path according do that

you can do it like

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

where |DataDirectory| word will automatically Get path of exe from where its executing

thanks

shoaib mansoori replied to Kirtan Patel on 08-Jun-11 07:22 AM
again thanks sir
you mean say that the path you will provide same as copy nothing to be change.
 currently me change the path as below
 public static string Con = @"Data Source=.;AttachDbFilename=E:\My Prac\WindowsFormsApplication1\WindowsFormsApplication1\DataBase\Al.mdf;Initial Catalog=AlHamd;Integrated Security=True";

this code in program.cs file bcoz of my window application

my database name is Al.mdf
this is your code so what i do same copy nothing to be change,
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
Aniket replied to shoaib mansoori on 23-Sep-11 10:56 AM
Thank s