Tuesday, January 20, 2009

Attaching a database without (.ldf) file

If you don't have the log (.ldf) file for an SQL server database, and you need to attach the database into sql server 2005, then run this sql query from SQL server 2005:

sp_attach_single_file_db N'OMS', N'E:\DataBase\OMS.mdf'

The first part is the name of the database and the second part is the physical address of the (.mdf) file. "N" prefix represents National Language Character Set. Which means the subsequent string is in unicode.

Alternatively, you can directly add a Database into a visual studio project. For this, you need to add an entry in the app.config file:

<add name="WpfDataEntry.Properties.Settings.OMSConnectionString"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\OMS.mdf;
Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />

The |Datadirectory| is a variable which holds the location of the root folder for databases. For ASP .NET web applications, the name of the folder is App_data. for desktop applications, the folder is the "Bin" folder

Credit for this post goes to Tanveer vai (Tanveer Ibn Haresh, Software Engineer, Bording Vista Ltd.)

No comments: