Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何知道行是否已更新_C#_Sql - Fatal编程技术网

C# 如何知道行是否已更新

C# 如何知道行是否已更新,c#,sql,C#,Sql,我需要阻止用户在应用程序上写别人的批准。我已经输入Where IS NULL,它可以阻止它覆盖数据,但是我需要显示一个标签,上面写着记录未更新为已批准 如何知道sql查询是否已更新,并获取该信息,以便执行if语句 If row updated then label.text = "data approved" else label.text = "You can not approve this as it's already approved" CheckBox

我需要阻止用户在应用程序上写别人的批准。我已经输入Where IS NULL,它可以阻止它覆盖数据,但是我需要显示一个标签,上面写着记录未更新为已批准

如何知道sql查询是否已更新,并获取该信息,以便执行if语句

If row updated then 
    label.text = "data approved" 
else   
    label.text = "You can not approve this as it's already approved"


CheckBox ckb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)ckb.Parent.Parent;           //Get row selected
        int idx = row.RowIndex;

        //Get activity number ID for update query
        Label lblDHActivityNoApp = (Label)row.Cells[0].FindControl("lblDHActivityNoApp");
        string Number = lblDHActivityNoApp.Text.ToString();

        string connectionString = ConfigurationManager.ConnectionStrings["PlusConnectionString"].ConnectionString;
        string UpdateSql = "UPDATE Activities SET Status = @Status, ApprovedBy=@ApprovedBy, ApprovedDate=@ApprovedDate WHERE ApprovedBy IS NULL and (ActivityNo=@No)";
        //Create SQL connection
        SqlConnection con = new SqlConnection(connectionString);

        //Create SQL Command And Sql Parameters
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = UpdateSql;

        SqlParameter Status = new SqlParameter("@Status", SqlDbType.VarChar, 40);
        Status.Value = "Approved";
        cmd.Parameters.Add(Status);

        SqlParameter ApprovedDate = new SqlParameter("@ApprovedDate", SqlDbType.VarChar, 50);
        DateTime myDateTime = DateTime.Now; ;
        ApprovedDate.Value = myDateTime.ToString("dd/MM/yy HH:mm:ss");
        cmd.Parameters.Add(ApprovedDate);

        SqlParameter ApprovedBy = new SqlParameter("@ApprovedBy", SqlDbType.VarChar, 40);
        ApprovedBy.Value = (string)Session["Username"];
        cmd.Parameters.Add(ApprovedBy);

        SqlParameter No = new SqlParameter("@No", SqlDbType.VarChar, 50);
        No.Value = Number;
        cmd.Parameters.Add(No);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            gbDHdetailsApp.DataBind();  //Reloads gridview with checked data       
        }

        catch (SqlException ex)
        {
            string errorMessage = "Not approved, please contact support";
            errorMessage += ex.Message;
            throw new Exception(errorMessage);
        }

        finally
        {
            con.Close();
        }

ExecuteNonQuery
返回受影响的行数。如果该值为0,则可以抛出
异常

ExecuteNonQuery
返回受影响的行数。如果这是0,您可以抛出一个
异常

ExecuteNonQuery()
返回受影响的行数,并且返回类型为
INT
,因此您可以执行以下操作

int rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected > 0)
{
  // do something
}
ExecuteNonQuery()
返回受影响的行数,并具有返回类型
INT
,因此您可以这样做

int rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected > 0)
{
  // do something
}