|
求助一个关于数据库恢复的,C# winform
有一个例子 我怎么都不成功
总是弹出还原失败,请确保还原项与库对应 或者 无法打开登录所请求的数据库 "MyDB"。登录失败,用户 'sa' 登录失败。
private void button1_Click(object sender, EventArgs e)
{
if (this.hy.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.hy.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
Restore();
}
private void Restore()
{
string path = this.textBox1.Text; //获得备份路径及数据库名称
string dbname = this.comboBox1.Text;string SqlStr1 = "Server=(local);database='" + this.comboBox1.Text + "';User ID=sa;Password=sa";
string SqlStr2 = "restore database " + dbname + " from disk='" + path + "'";
using (SqlConnection con = new SqlConnection(SqlStr1))
{
con.Open();
try
{
SqlCommand cmd = new SqlCommand(SqlStr2, con);
cmd.Connection = con;
cmd.ExecuteNonQuery();
MessageBox.Show("还原数据成功");
}
catch (Exception error)
{
MessageBox.Show("还原失败,请确保还原项与库对应");
}
finally
{
con.Close();
}
}
}
|
|