Hello Friends, I think I found the answer and here it is. I wrote this code in the event of clicking a button named attach database:
private void BTNAttach_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
con.Open();
string str = "use master;" +
"EXECUTE sp_attach_db @dbname = N'MyLibraryDataBase' , " +
" @filename1 = N'" + System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase.mdf'," +
"@filename2 = N'" + System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase_log.ldf'";
LAMDFSQLAdress.Text = System.Environment.CurrentDirectory+"\\Data\\MyLibraryDataBase.mdf";
LALOGSQLAdress.Text = System.Environment.CurrentDirectory + "\\Data\\MyLibraryDataBase_log.ldf";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("DataBase Successfully Attached.");
}
catch (Exception x)
{
if (x.Message.IndexOf("already exists") >= 0)
MessageBox.Show("DataBase already exists.");
else
MessageBox.Show(x.Message);
}
}