database
  • tsql
  • executenonquery
  • 2017-09-20 2 views 0 likes 
    0

    데이터베이스에서 값을 삭제하는 방법을 썼지 만 영향을받은 행을 확인하면 값은 0이고 처음 if 문으로 계속 들어가는가?데이터베이스에서 삭제

     private void workstationDelete() 
        { 
         string query = "DELETE FROM test_revision2 where wsid = @wsid and location = '@location'"; 
         try 
         { 
          conn = new SqlConnection(connStr); 
          conn.Open(); 
    
          SqlCommand cmd = new SqlCommand(query, conn); 
          cmd.Parameters.AddWithValue("@wsid", Convert.ToInt32(wsid2text.Text)); 
          cmd.Parameters.AddWithValue("@location", deletelocation); 
          cmd.ExecuteNonQuery(); 
    
    
          int rowsaffected = cmd.ExecuteNonQuery(); 
    
          if (rowsaffected == 0) 
          { 
           lblDel.Text = "Sorry Workstation: " + wsid2text.Text + " does not exist in " + deletelocation + ". Therefore it cannot be removed."; 
          } 
          else 
          { 
           lblDel.Text = "You have successfully removed Workstation: " + Convert.ToInt32(wsid2text.Text) + " in " + deletelocation; 
          } 
    
          conn.Close(); 
    
         } 
    
         catch (SqlException) 
         { 
          lblDel.Text = "randome"; 
         } 
        } 
    

    답변

    0

    '@location'에서 작은 따옴표를 제거하십시오. 매개 변수를 무시하고 문자열 리터럴과 일치 시키려고합니다.

    기타 : good reasonsAddWithValue을 사용하지 마십시오.

    0

    문제는 cmd.executeNonQuery를 두 번 사용했지만 첫 번째가 1로 돌아 왔지만 var에 초기화되면 0이됩니다. 한 번 실행하지 않은 쿼리를 사용하면 int var.

    관련 문제