}
else if (System.IO.File.Exists(strLdfFilePath))
{
sb.Append(",@filename2='" + strLdfFilePath + "'");
AttachDataBase(sb.ToString());
}
else
{
Console.WriteLine("数据库文件" + strMdfFilePath + "缺少必备的日志文件!");
}
}
}
}
///
/// 连接数据库并执行附加对应的数据库文件命令
///
/// 附加数据库命令字符串
///
private bool AttachDataBase(string strSql)
{
SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=master;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
//如果数据库中存在名为要添加的数据库时则抛出异常
Console.WriteLine("附加数据库时异常:" + ex.Message);
return false;
}
finally
{
con.Close();
}
}