对象或列名称缺失或为空(0x80131904)

时间:2018-02-20 16:26:55

标签: c# sql-server database exception mdf

当我尝试使用该按钮恢复数据库时,我收到此错误:

  

"对象或列名称缺失或为空"。

问题是我正在使用脱机数据库文件(.mdf),当我在此错误上单击确定并再次按下恢复按钮时,它就像一个魅力。 代码是:

private void RestoreButton_Click(object sender, EventArgs e)
{
    string database = con.Database.ToString();
    if (con.State!=ConnectionState.Open)
    {
        con.Open();
    }
    try
    {
        string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
        SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
        bu2.ExecuteNonQuery();

        string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
        SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
        bu3.ExecuteNonQuery();

        string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
        SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
        bu4.ExecuteNonQuery();

        MessageBox.Show("Database succesfully restored!");
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

There's the full error.

0 个答案:

没有答案