executiontenonquery不在for循环中工作

时间:2013-04-10 10:02:11

标签: c# asp.net

在数据库中:

Alter Procedure Update_MaterialTransactionsto2ForWithdrawal
  @materialName varchar(50),
  @staffNumber varchar(10),
  @description varchar(50),
  @transactionID int
As
Begin
   Update Table_MaterialTransactions
   set Status=2
   where StaffNumber = @staffNumber 
     and CrossSection = @description 
     and SubSubCategory = @materialName 
     and Status = 1 
     and TransactionID = @transactionID
End

在数据访问层中:

public static void UpdateMaterial(string staffNumber,string materialName,string description,int transaction)
{
    SqlConnection connection = new SqlConnection(ConnectDatabase.ReturnConnectionString());
    //I am passing connection string as the parameter

    try
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand("Update_MaterialTransactionsto2ForWithdrawal", connection);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Clear();
        cmd.Parameters.Add("@materialName", SqlDbType.Varchar, 50).Value = materialName;
        cmd.Parameters.Add("@staffNumber", SqlDbType.Varchar, 50).Value = staffNumber;
        cmd.Parameters.Add("@description", SqlDbType.Varchar, 50).Value = description;
        cmd.Parameters.Add("@transactionID", SqlDbTypeInt).Value = transactionID;

        int i = cmd.ExecuteNonQuery();

        connection.Close();
    }
    catch(Exception ex)
    {
       connection.Close();
    }

在客户端:

void btnSubmit_Click(Object sender,EventArgs e)
{
   int j=0,k=0;

   for(int i=0;i<transactions.Count;i++)
   {
       string id = "ctl00$ContentPlaceHolder1$" + i.ToString();

       CheckBox chk=(CheckBox)Page.FindControl(id);

       if(chk.Checked == true)
       {
            Objects.UpdateMaterial(staffNumbers[i].ToString(), materials[i].ToString(), descriptions[i].ToString(), Convert.ToInt32(transactions[i]));

            j++;
       }
       else
       {
            Objects.DeleteTheSelectedRowOfMaterialTransaction(staffNumbers[i].ToString(), materials[i].ToString(), descriptions[i].ToString(), Convert.ToInt32(transactions[i]));

            k++;
        }
   }

我在表格中有复选框,当用户选中复选框并单击提交时,选中的复选框将更新数据库。

但是cmd.ExecuteNonQuery()没有执行,它返回0行。它没有抛出任何错误。但是如果我在数据库中手动执行此操作,则存储过程正常工作。

请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

尝试在程序中添加最后一行

Return @@Rowcount