為什麼運行不了呢 他返回的信息是未能找到儲存過程kill 51 幫幫忙看一下
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("請選擇要還原的數据庫路徑");
            }
            try
            {
                if (DataManagement.my_con.State == ConnectionState.Open)
                {
                    DataManagement.my_con.Close();
                }
                   string datastr = "server=JJCHICK.;database=master;uid=sa;pwd=";
                   SqlConnection conn = new SqlConnection(datastr);
                   conn.Open();
                   string strsql = "select spid from master..sysprocesses where dbid = db_id('porperty')";
                   SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
                   DataTable spidtable = new DataTable();
                   da.Fill(spidtable);
                   SqlCommand cmd = new SqlCommand();
                   cmd.CommandType = CommandType.Text;
                   cmd.Connection = conn;
                   for (int irow = 0; irow < spidtable.Rows.Count - 1; irow++)
                   {
                       cmd.CommandText = "kill" + spidtable.Rows[irow][0].ToString();
                       cmd.ExecuteNonQuery();
                   }
                   conn.Close();
                   conn.Dispose();
                   SqlConnection temcon = new SqlConnection(DataManagement.m_str_sqlcon);
                   temcon.Open();
                   SqlCommand sqlcom = new SqlCommand("backup database porperty to disk='" 
                   + textBox3.Text.Trim() + "'use master restore database porperty from disk='"
                   + textBox3.Text.Trim() + "'", temcon);
                   sqlcom.ExecuteNonQuery();
                   sqlcom.Dispose();
                   temcon.Close();
                   temcon.Dispose();
                   MessageBox.Show("數据庫還原成功");
                   MessageBox.Show("關閉系統");
                   Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }另外datamanagement類代碼
class DataManagement
    {
        #region  全局變量
        public static string temptext = "";
        public static string m_str_sqlcon = "server=JJCHICK.;database=porperty;uid=;pwd=";
        public static SqlConnection my_con;
        #endregion;
        public static SqlConnection getcon()
        {
            my_con = new SqlConnection(m_str_sqlcon);
            my_con.Open();
            return my_con;
        }
        public SqlDataReader getcom(string sqlstr)
        {
            getcon();
            SqlCommand cmd = my_con.CreateCommand();
            cmd.CommandText = sqlstr;
            SqlDataReader my_read = cmd.ExecuteReader();
            return my_read;
        }
        public void getsqlcom(string sqlstr)
        {
            getcon();
            SqlCommand sqlcom = new SqlCommand(sqlstr, my_con);
            sqlcom.ExecuteNonQuery();
            sqlcom.Dispose();
            my_con.Close();
        }
    }本人覺得是數据庫的問題那個什麼master.不知如果亂試又可以save 但重開程序和數据庫又不行 ..又找不到kill 51

解决方案 »

  1.   

    我是跟書的~~ 就改了數据庫名字.. 應該正確的...
    我也用過別的方法 都是找不到kill 51 大家幫幫忙呀
      

  2.   

    复原前,是否 有旧数库?及进程
    最好有SQL语句执行一下
      

  3.   

    他是有進程 你說得對呀...但是說要殺掉時又找不到
    string strsql = "select spid from master..sysprocesses where dbid = db_id('porperty')"; //意思是在master數据庫中找到子系統(porperty)中的所有進程
                      SqlDataAdapter da = new SqlDataAdapter(strsql, conn); 
                      DataTable spidtable = new DataTable(); 
                      da.Fill(spidtable); 
                      SqlCommand cmd = new SqlCommand(); 
                      cmd.CommandType = CommandType.Text; 
                      cmd.Connection = conn; //到以上為止是建一個表打所有進程編號方進去
                      for (int irow = 0; irow < spidtable.Rows.Count - 1; irow++) 
                      { 
                          cmd.CommandText = "kill" + spidtable.Rows[irow][0].ToString(); 
                          cmd.ExecuteNonQuery(); //就是這出錯..他說找不到要殺的進程..
                      } 
    正常情況下 在上邊找到 下邊殺掉 就正常 但是在下邊他又說找不到..所以才出錯
    我用messagebox 調試過spidtable.Rows是有值的..但就是找不到要殺的...