回复 1# 的帖子
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("database=master;user id=sa;password=sa;data source=(local);pooling=false");
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
sp_attach_db @dbname,@filename1,@filename2";
SqlParameter para=cmd.Parameters.Add("@dbname",SqlDbType.VarChar,20);
para.Value="abc";
para=cmd.Parameters.Add("@filename1",SqlDbType.NVarChar,260);
para.Value=Application.StartupPath+"\\abc_Data.MDF";
para=cmd.Parameters.Add("@filename2",SqlDbType.NVarChar,260);
para.Value=Application.StartupPath+"\\abc_Log.LDF";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("已成功附加数据库");
}
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("database=master;user id=sa;password=sa;data source=(local);pooling=false");
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
sp_detach_db @dbname,@skipchecks";
SqlParameter para=cmd.Parameters.Add("@dbname",SqlDbType.VarChar,20);
para.Value="abc";
para=cmd.Parameters.Add("@skipchecks",SqlDbType.NVarChar,10);
para.Value="true";
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException er)
{
MessageBox.Show(er.Message);
return;
}
con.Close();
MessageBox.Show("已成功分离数据库");
}