2012-03-13 101 views
0

我有下面的代碼,我想要做一個計數,如果(計數)== 0比插入其他update.I似乎無法弄清楚什麼是錯誤的頁面應該如果查詢完成但轉到下一頁,但頁面只能重新加載,並且沒有插入或更新數據。當我按下按鈕時應執行此操作。您可以幫我解決這個問題嗎?我問過,但沒有answer.I現在由我自己嘗試過,但似乎無法看到什麼是problem.Sorry的重複計數然後插入其他更新

string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
      if (string.IsNullOrEmpty(ip)) 
      { 
       ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      } 


      String id_sesiune; 
      id_sesiune = Session.SessionID; 

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString); 
      con.Open(); 
      SqlCommand cmd1 = new SqlCommand("SELECT count(*) from Raspunsuri where id_intrebare=2",con); 

      int read = Convert.ToInt16(cmd1.ExecuteScalar()); 


      if (read == 0) 
      { 
       SqlCommand cmd = new SqlCommand("INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,'2',@ip,@idsesiune)", con); 
       cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]); 
       cmd.Parameters.AddWithValue("@raspuns", textbox2.Text); 
       cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime()); 
       cmd.Parameters.AddWithValue("@ip", ip); 
       cmd.Parameters.AddWithValue("@idsesiune", id_sesiune); 

       try 
       { 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        Response.Redirect("User3.aspx"); 
       } 

       catch (Exception ex) 
       { 
        Console.WriteLine("Error:" + ex); 
       } 
       finally 
       { 
        con.Close(); 
       } 


      } 

      else 
      { 

       SqlCommand cmd = new SqlCommand("UPDATE Raspunsuri SET [email protected],[email protected],[email protected],id_intrebare=2,[email protected],[email protected] WHERE id_intrebare=2", con); 
       cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]); 
       cmd.Parameters.AddWithValue("@raspuns", textbox2.Text); 
       cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime()); 
       cmd.Parameters.AddWithValue("@ip", ip); 
       cmd.Parameters.AddWithValue("@idsesiune", id_sesiune); 

       try 
       { 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        Response.Redirect("User3.aspx"); 
       } 

       catch (Exception ex) 
       { 
        Console.WriteLine("Error:" + ex); 
       } 
       finally 
       { 
        con.Close(); 
       } 

      } 
+0

是否有例外? 'select count(*)from Raspunsuri where id_intrebare = 2' returns any rows? – sll 2012-03-13 20:16:02

+0

如果不返回我想要插入其他更新.. – Rares 2012-03-13 20:17:33

+0

如果沒有行返回並步入更新部分。它不會拋出錯誤,但不會插入錯誤和數據。 – Turbot 2012-03-13 20:17:41

回答

3

當我陷入這種情況,我會恢復到一個存儲程序。

CREATE PROCEDURE UpsertRasPunsuri(@keyID int, @cnp int, @raspuns nvarchar(30), 
        @data smalldatetime, @ip nvarchar(30), @idsesiune int) 
    AS 
    BEGIN 
     declare @cnt int 
     SELECT @cnt = count(*) from Raspunsuri where [email protected] 
     if @cnt = 0 
      INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,2,@ip,@idsesiune) 
     else 
      UPDATE Raspunsuri SET 
        [email protected],[email protected],[email protected], 
        id_intrebare=2,[email protected],[email protected] 
      WHERE [email protected]  
    END 

現在你的代碼簡化了這種方式

try 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("UpsertRasPunsuri",con); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@keyID", 2); 
    cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]); 
    cmd.Parameters.AddWithValue("@raspuns", textbox2.Text); 
    cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime()); 
    cmd.Parameters.AddWithValue("@ip", ip); 
    cmd.Parameters.AddWithValue("@idsesiune", id_sesiune); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    Response.Redirect("User3.aspx"); 
} 

catch (Exception ex) 
{ 
    Console.WriteLine("Error:" + ex); 
} 
finally 
{ 
    con.Close(); 
} 

當然,我在這裏做了很多假設,在你的桌子上,列類型和長度。
但是,這將容易適應。現在,如果您從VS或SSMS運行存儲過程,則可以排除數據訪問代碼中的錯誤。

+0

但沒有程序? – Rares 2012-03-13 20:59:13