نوشته شده توسط
reza_mostafavi63
................................
داری تلافی می کنی....
FileInfo mdf = new FileInfo(dbfile);
databasename = mdf.Name.ToLower().Replace(@".mdf", @"");
StringCollection databasefiles = new StringCollection();
databasefiles.Add(mdf.FullName);
databasefiles.Add(mdf.FullName.ToLower().Replace(@ ".ldf", @""));
//this is where I have issue. Obviously I can't assume that the log file name would be the same as mdf file name with ldf extension. Thats when I thought there would be a way to read the header information from mdf file, and that will have ldf information.
Server sqlServer = new Server(textServer.Text);
sqlServer.AttachDatabase(databasename, databasefiles);
منبع
راه دیگه از CodeProject:
string mypath = string.Empty;
mypath = Application.StartupPath.ToString();
SqlConnection attachCon = new SqlConnection("server =(local);database=master;Integrated Security=True");
SqlCommand cm = new SqlCommand();
cm.CommandType = CommandType.Text;
cm.CommandText = "sp_attach_db @dbname = N'Metal', " +
" @filename1 = N'" + mypath + "\\DB\\Test_Data.MDF" + "'," +
"@filename2 = N'" + mypath + "\\DB\\Test_Log.LDF" + "'";
attachCon.Open();
cm.Connection = attachCon;
cm.ExecuteNonQuery();