下面是单击更新按钮后更新行的方法。我的SQL Server存储过程在更新之前期望现有公司名称,新公司名称以及是否存在。大胆的部分是它破碎的地方。当我选择编辑时,我想在更新之前解析公司名称行中的当前值。
protected void CompanyTable_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand("[updateCompanyName]", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
GridViewRow row = CompanyTable.SelectedRow;
cmd.Parameters.AddWithValue("@CurrentCompanyName", CompanyTable.Rows[e.RowIndex].Cells[0].Controls[0]);
cmd.Parameters.AddWithValue("@NewCompanyName", CompanyInputTextBox.Text).Direction = ParameterDirection.Input;
SqlParameter objisExists = new SqlParameter("@isExists", SqlDbType.Int);
objisExists.Direction = ParameterDirection.Output;
cmd.Parameters.Add(objisExists);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
int isExists = Convert.ToInt32(cmd.Parameters["@isExists"].Value.ToString());
if (isExists == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "AddCompanySuccess();", true);
}
else if (isExists == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "CompanyExistsValidation();", true);
}
}
}
存储过程参数:
ALTER PROCEDURE [dbo].[updateCompanyName]
@CurrentCompanyName VARCHAR(50),
@NewCompanyName VARCHAR(50),
@IsExists INT = 0 OUT
我在SQL中的更新:
DECLARE @CompanyID INT
SELECT @CompanyID = CompanyID
FROM company
WHERE companyname = @CurrentCompanyName
BEGIN
IF EXISTS (SELECT CompanyName FROM company
WHERE companyname = @NewCompanyName )
BEGIN
SET @IsExists = 1
END
ELSE
BEGIN
UPDATE COMPANY
SET CompanyName = @NewCompanyName
WHERE companyid = @CompanyID
SET @IsExists = 0
END
END
PRINT @isexists