如何用C#在数据库中写入NULL



我有A表和B表。B表每天根据A表的变化进行更新。在A表中,2个实体(如:SCD_DEPT(字符串值(和LEAVE_DATE(日期时间((和在A表的空值看起来为"NULL",而在B表的空SCD_DEPT值看起来为空。我想清空SCD_DEPT值,在B表中看起来为"NULL">

这是我代码中的更新部分

var employeequery = $"{UpdateQuery} EMAIL=@EMAIL, GID = @GID, SAP_COMPANY_CODE=@SAP_COMPANY_CODE, FIRST_NAME=@FIRST_NAME, LAST_NAME = @LAST_NAME, COST_CENTER=@COST_CENTER, MANAGER_EMPLOYEE_ID=@MANAGER_EMPLOYEE_ID, SCD_DEPT=@SCD_DEPT, LEAVE_DATE= @LEAVE_DATE, START_DATE=@START_DATE, CONTRACT_TYPE=@CONTRACT_TYPE, MONTHLY_WORKING_DAY=@MONTHLY_WORKING_DAY, STATUS=@STATUS, LAST_UPDATE_TIME=@LAST_UPDATE_TIME  WHERE EMPLOYEE_ID = @EMPLOYEE_ID";
            var command = DataAccess.Instance.CreateCommand(employeequery);
            command.Parameters.AddWithValue("@EMPLOYEE_ID", entity.Id);
            command.Parameters.AddWithValue("@EMAIL", entity.Email);
            command.Parameters.AddWithValue("@GID", entity.Gid);
            command.Parameters.AddWithValue("@SAP_COMPANY_CODE", entity.SapCompanyCode);
            command.Parameters.AddWithValue("@FIRST_NAME", entity.FirstName);
            command.Parameters.AddWithValue("@LAST_NAME", entity.LastName);
            command.Parameters.AddWithValue("@COST_CENTER", entity.CostCenterName);
            command.Parameters.AddWithValue("@MANAGER_EMPLOYEE_ID", entity.ManagerEmployeeID);
            if(entity.ScdDept == "  ")
            {
                command.Parameters.AddWithValue("@SCD_DEPT", DBNull.Value);
            }
            else
            {
                command.Parameters.AddWithValue("@SCD_DEPT", entity.ScdDept);
            }
                command.Parameters.AddWithValue("@LEAVE_DATE", entity.LeaveDate);
            command.Parameters.AddWithValue("@START_DATE", entity.StartDate);
            command.Parameters.AddWithValue("@CONTRACT_TYPE", entity.ContractType);
            command.Parameters.AddWithValue("@MONTHLY_WORKING_DAY", entity.MonthlyWorkingDay);
            command.Parameters.AddWithValue("@STATUS", entity.Status);
            command.Parameters.AddWithValue("@LAST_UPDATE_TIME", entity.LastUpdateTime);

我为SCD_DEPT(字符串值(尝试if else,但它不起作用。我想要更有效的工作方式。谢谢你的帮助。

我想在检查之前没有将entity.ScdDept与"@SCP_DEPT"链接,或者它在上面的某个地方创建?如果是,则应将if(entity.ScdDept == " ")更改为if(entity.ScdDept == null)。请试试,然后回信。

使用string.IsNullOrWhiteSpace(entity.ScdDept?.Trim())

试试这个:

var employeequery = $"{UpdateQuery} EMAIL=@EMAIL, GID = @GID, SAP_COMPANY_CODE=@SAP_COMPANY_CODE, FIRST_NAME=@FIRST_NAME, LAST_NAME = @LAST_NAME, COST_CENTER=@COST_CENTER, MANAGER_EMPLOYEE_ID=@MANAGER_EMPLOYEE_ID, SCD_DEPT=NULLIF(@SCD_DEPT, ''), LEAVE_DATE= @LEAVE_DATE, START_DATE=@START_DATE, CONTRACT_TYPE=@CONTRACT_TYPE, MONTHLY_WORKING_DAY=@MONTHLY_WORKING_DAY, STATUS=@STATUS, LAST_UPDATE_TIME=@LAST_UPDATE_TIME  WHERE EMPLOYEE_ID = @EMPLOYEE_ID";
var command = DataAccess.Instance.CreateCommand(employeequery);
    command.Parameters.AddWithValue("@EMPLOYEE_ID", entity.Id);
    command.Parameters.AddWithValue("@EMAIL", entity.Email);
    command.Parameters.AddWithValue("@GID", entity.Gid);
    command.Parameters.AddWithValue("@SAP_COMPANY_CODE", entity.SapCompanyCode);
    command.Parameters.AddWithValue("@FIRST_NAME", entity.FirstName);
    command.Parameters.AddWithValue("@LAST_NAME", entity.LastName);
    command.Parameters.AddWithValue("@COST_CENTER", entity.CostCenterName);
    command.Parameters.AddWithValue("@MANAGER_EMPLOYEE_ID", entity.ManagerEmployeeID);
    command.Parameters.AddWithValue("@SCD_DEPT", entity.ScdDept);
    command.Parameters.AddWithValue("@LEAVE_DATE", entity.LeaveDate);
    command.Parameters.AddWithValue("@START_DATE", entity.StartDate);
    command.Parameters.AddWithValue("@CONTRACT_TYPE", entity.ContractType);
    command.Parameters.AddWithValue("@MONTHLY_WORKING_DAY", entity.MonthlyWorkingDay);
    command.Parameters.AddWithValue("@STATUS", entity.Status);
    command.Parameters.AddWithValue("@LAST_UPDATE_TIME", entity.LastUpdateTime);

我这样解决了这个问题:

var employeequery = $"{UpdateQuery} EMAIL=@EMAIL, GID = @GID, SAP_COMPANY_CODE=@SAP_COMPANY_CODE, FIRST_NAME=@FIRST_NAME, LAST_NAME = @LAST_NAME, COST_CENTER=@COST_CENTER, MANAGER_EMPLOYEE_ID=@MANAGER_EMPLOYEE_ID, SCD_DEPT=@SCD_DEPT, LEAVE_DATE= @LEAVE_DATE, START_DATE=@START_DATE, CONTRACT_TYPE=@CONTRACT_TYPE, MONTHLY_WORKING_DAY=@MONTHLY_WORKING_DAY, STATUS=@STATUS, LAST_UPDATE_TIME=@LAST_UPDATE_TIME  WHERE EMPLOYEE_ID = @EMPLOYEE_ID";
            var command = DataAccess.Instance.CreateCommand(employeequery);
            command.Parameters.AddWithValue("@EMPLOYEE_ID", entity.Id);
            command.Parameters.AddWithValue("@EMAIL", entity.Email);
            command.Parameters.AddWithValue("@GID", entity.Gid);
            command.Parameters.AddWithValue("@SAP_COMPANY_CODE", entity.SapCompanyCode);
            command.Parameters.AddWithValue("@FIRST_NAME", entity.FirstName);
            command.Parameters.AddWithValue("@LAST_NAME", entity.LastName);
            command.Parameters.AddWithValue("@COST_CENTER", entity.CostCenterName);
            command.Parameters.AddWithValue("@MANAGER_EMPLOYEE_ID", entity.ManagerEmployeeID);
            if (entity.ScdDept == "") command.Parameters.AddWithValue("@SCD_DEPT", DBNull.Value);
            else command.Parameters.AddWithValue("@SCD_DEPT", entity.ScdDept);
            if (entity.LeaveDate == DateTime.MaxValue) command.Parameters.AddWithValue("@LEAVE_DATE", DBNull.Value);
            else command.Parameters.AddWithValue("@LEAVE_DATE", entity.LeaveDate);
            command.Parameters.AddWithValue("@START_DATE", entity.StartDate);
            command.Parameters.AddWithValue("@CONTRACT_TYPE", entity.ContractType);
            command.Parameters.AddWithValue("@MONTHLY_WORKING_DAY", entity.MonthlyWorkingDay);
            command.Parameters.AddWithValue("@STATUS", entity.Status);
            command.Parameters.AddWithValue("@LAST_UPDATE_TIME", entity.LastUpdateTime);

谢谢你抽出时间。

相关内容

  • 没有找到相关文章

最新更新