实例如下
/// <summary>
/// 附加数据库
/// </summary>
/// <returns></returns>
private bool Attachdb()
{
try
{
string sql = string.Empty;
if (File.Exists(sMDBFile))
{
sql = "EXEC sp_attach_db @dbname = '" + txtDBName.Text +
"', @filename1 = '" + sMDBFile+
"',@filename2='" + sLog + "'";
SqlConnection conn = new SqlConnection(string.Format("Data Source={0};Initial Catalog=master;User ID={1};PWD={2}","(local)","sa","pwd"));
using (conn)
{
lock (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
}
return true;
}
catch
{
return false;
}
}
//sMDBFile 为mdf文件路径
//sLog 为ldf文件路径
/// <summary>
/// 附加数据库
/// </summary>
/// <returns></returns>
private bool Attachdb()
{
try
{
string sql = string.Empty;
if (File.Exists(sMDBFile))
{
sql = "EXEC sp_attach_db @dbname = '" + txtDBName.Text +
"', @filename1 = '" + sMDBFile+
"',@filename2='" + sLog + "'";
SqlConnection conn = new SqlConnection(string.Format("Data Source={0};Initial Catalog=master;User ID={1};PWD={2}","(local)","sa","pwd"));
using (conn)
{
lock (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
}
return true;
}
catch
{
return false;
}
}
//sMDBFile 为mdf文件路径
//sLog 为ldf文件路径