SQL问题 - 不读取NULL值

时间:2016-08-16 07:40:38

标签: c# winforms sybase-asa

当我尝试运行代码时出现问题,当我设置PASSWORD ='''如下面的代码。

public int ClearEmployeePasswordById(Employee p)
{
    int result = -1;        
    try
    {               
        string sql = "UPDATE EMPLOYEE SET " +
                     "PASSWORD=''" +
                     "WHERE Employee_Id=" + p.EmployeeId;

        Common.logToFile("PosNet.Data.Sybase.cs", "ClearEmployeePasswordById", sql);
        string r = ExecuteNonQuery(sql);

        if (!string.IsNullOrEmpty(r))
            throw new Exception(r);

        result = 1;
        InsertAuditLog();
    }
    catch (Exception ex)
    {
        Common.logErrorToFile("PosNet.Data.Sybase.cs", "ClearEmployeePasswordById", ex.Message);
        //throw ex;
    }
    return result;
}   

但是当我使用sql更改我的函数的PASSWORD=NULL时,它会显示错误"Reset Password Failed"。它只是读取空字符串而不是NULL值。为什么会这样?

private void btnPasswordReset_Click(object sender, EventArgs e)
{
    try
    {
        string employeeWithoutQuote = lblEmployeeId.Text.Substring(1, 10);
        int employeeId = int.Parse(employeeWithoutQuote);
        Employee employee = MF.BC.DA.GetEmployeeByBarcode(employeeId);
        if (MF.BC.DA.ClearEmployeePasswordById(employee) != 1)
        {
            throw new Exception("Reset Password Failed");
        }
        else
        {
            MessageBox.Show("Reset Password Success");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

1 个答案:

答案 0 :(得分:2)

将SQL str更改为:

string sql = 
    "UPDATE EMPLOYEE SET " +
    "PASSWORD=NULL " + //NOTE: Added a space
    "WHERE Employee_Id=" + p.EmployeeId;

没有空间,你的最终SQL是错误的。