2012-01-04 71 views
1

我已經編寫了一個簡單的存儲過程來更新記錄,但該記錄不工作,但我無法解決原因。不會拋出異常,但記錄也不會更新。從C#調用的存儲過程不更新記錄

請參見下面的代碼:在SQL Server

public int IMGId; 

protected void btnUpdate_Click(object sender, EventArgs e) 
    { 
     string result = ""; 
     string sSQL = "usp_imageloader_update"; 


     using (SqlConnection dbConnection = new SqlConnection(CKS_app_settings.sql_conn_string_db)) 
     { 
      // SqlTransaction tn=null; 
      try 
      { 
       dbConnection.Open(); 
       //start Transaction 
       // tn = dbConnection.BeginTransaction(); 
       SqlCommand command = new SqlCommand(sSQL, dbConnection); 
       //command.Transaction = tn; 
       command.CommandText = sSQL; 
       command.CommandType = CommandType.StoredProcedure; 
       command.CommandTimeout = 1024; 
       command.Parameters.AddWithValue("@p_image_id", IMGId); 
       command.Parameters.AddWithValue("@p_url", txtUrl.Text); 
       command.Parameters.AddWithValue("@p_alt_text", txtAlt.Text); 
       //command.Parameters.AddWithValue("@p_filepath", File1.Value); 
       //command.Parameters.AddWithValue("@p_cntr_id", str_id); 
       int rowsAffected = command.ExecuteNonQuery(); 
      } 
      catch (SqlException ex) 
      { 
       // throw ex; 

       //If it failed for whatever reason, rollback the //transaction 
       //tn.Rollback();       
       //No need to throw because we are at a top level call and //nothing is handling exceptions 
       result = ex.InnerException.Message; 
      } 
     } 

存儲過程

USE [smsdb_test_griffin2] 
GO 
/****** Object: StoredProcedure [dbo].[usp_imageloader_update] Script Date: 01/04/2012 09:05:41 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER procedure [dbo].[usp_imageloader_update] 
@p_image_id INT, 
@p_url VARCHAR(255), 
@p_alt_text VARCHAR(255) 
as 

UPDATE Image_Library_UK 
SET 
[email protected]_url, 
[email protected]_alt_text 

WHERE [email protected]_image_id 
+7

如果您單獨執行它,即不是從您的C#代碼執行它,您的SP是否工作? – razlebe 2012-01-04 09:58:41

+0

你說的T-SQL? GO在T-SQL的存儲過程中不起作用。 – 2012-01-04 10:00:19

+0

您是否嘗試過使用C#中傳遞的值的SQL Managament中的exec過程? – Mariusz 2012-01-04 10:01:04

回答

1

嘗試過它孤立,假設這工作(並沒有什麼昭然若揭錯了),我會假定你的一個參數沒有被正確設置。這可能是因爲IMGid不正確 - 這會產生這種效果 - 或者說,由於頁面的負載,url和alttext已經被重置爲它們的值。

檢查調用點的值。這可能是您需要使用!Page.IsPostBack不在回發中重置這些值。您可能需要使用請求變量來訪問它們 - 它取決於您的其他代碼。

+0

我硬編碼的ID和存儲過程的int值的工作。我想從我的代碼中獲取id時出錯了。感謝所有幫助過的人! – 2012-01-04 10:32:11