问题
I have A table and B table. B table update everyday according to changes from A table. in A table 2 entities like : SCD_DEPT (String Value) and LEAVE_DATE(DateTime) and in A table empty values are looking "NULL" for SCD_DEPT and LEAVE_DATE but in B table empty SCD_DEPT values are looking empty. I want to empty SCD_DEPT values looks "NULL" in B table
it's my UPDATE part in my code
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);
I try if else for SCD_DEPT (string value) but it is not working.I want to more effective and working way. Thank you for all your helps.
回答1:
I suppose you don't link entity.ScdDept
with "@SCP_DEPT" before checking or it creates somewhere above? If it is you should change if(entity.ScdDept == " ")
onto if(entity.ScdDept == null)
. Try it please and write back.
回答2:
Use string.IsNullOrWhiteSpace(entity.ScdDept?.Trim())
回答3:
Try this:
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);
回答4:
I solved this problem like that:
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);
thank you for your time.
来源:https://stackoverflow.com/questions/60558141/how-can-i-write-the-null-in-database-with-c-sharp