-2

我有两个字段 DOB(出生日期)和(加入日期)如果用户输入日期,则在数据库中插入->日期,否则插入->空

我已将属性定义为

  public DateTime DOB
    {
        get;
        set;
    }
    public DateTime DOJ
    {
        get;
        set;
    }

现在在 save_click 事件中

        IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
        String datetime = txtDOB.Text.Trim();
        DateTime date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
        objEmp.DOB = date;

        provider = new System.Globalization.CultureInfo("en-CA", true);
        datetime = txtDOJ.Text.Trim();
        date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
        objEmp.DOJ = date;

类.cs

        param[7] = new Service.SqlParameter();
        param[7].ParameterName = "@DOB";
        param[7].Value = DOB;
        param[7].SqlDbType = Service.SqlDbType.DateTime;

        param[8] = new Service.SqlParameter();
        param[8].ParameterName = "@DOJ";
        param[8].Value = DOJ;
        param[8].SqlDbType = Service.SqlDbType.DateTime;

我希望当用户没有插入任何日期时,应该插入 null ......但是通过这段代码,我得到一个错误的 存储过程

@FK_EmployeeTypeID Numeric(18,0),
    @EmployeeName NVARCHAR(50), 
    @CellNo  NVARCHAR(50),  
    @PhNo  NVARCHAR(50),    
    @Address  NVARCHAR(Max),    
    @Email  NVARCHAR(50),   
    @DOB DATETIME,
    @DOJ DATETIME,
    @DOR NVARCHAR(12),
    @Status NVARCHAR(50),
    @Remarks NVARCHAR(Max)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    BEGIN TRY
        BEGIN TRANSACTION Employee_Insert
    INSERT 
    INTO
    Employee
    (
        FK_EmployeeTypeID,
        EmployeeName,   
        CellNo, 
        PhNo,   
        Address,    
        Email,  
        DOB,
        DOJ,
        DOR,
        Status,
        Remarks

    )
    VALUES
    (

        @FK_EmployeeTypeID,
        @EmployeeName,  
        @CellNo,    
        @PhNo,  
        @Address,   
        @Email, 
        @DOB,
        @DOJ,
        @DOR,
        @Status,
        @Remarks
    )

        COMMIT TRANSACTION Employee_Insert
    SELECT '1'
    END TRY
    BEGIN CATCH
        SELECT 'Error : ' + ERROR_MESSAGE()
        ROLLBACK TRANSACTION Employee_Insert
    END CATCH

网络服务

[WebMethod]
    public string InsUpdDel(string Conn, string ProcName, SqlParameter[] p)
    {
        try
        {
            using (SqlConnection cn = new SqlConnection(Conn))
            {
                if (cn.State == ConnectionState.Open || cn.State == ConnectionState.Broken || cn.State == ConnectionState.Connecting || cn.State == ConnectionState.Executing || cn.State == ConnectionState.Fetching)
                    cn.Close();
                cn.Open();
                SqlCommand cmd = new SqlCommand(ProcName, cn);

                cmd.CommandType = CommandType.StoredProcedure;

                foreach (SqlParameter param in p)
                {
                    cmd.Parameters.Add(param);
                }
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                if (dr[0].ToString() == "1")
                {
                    return "1";
                }
                else
                {
                    return dr[0].ToString();
                }
                dr.Close();
                cn.Close();
            }
        }
        catch (Exception ex)
        {
            return "Error : " + ex.Message.ToString();
        }
4

4 回答 4

1

此代码适用于在 sql 的日期时间字段中插入空值

cmd.Parameters.AddWithValue("@from_date", SqlDbType.DateTime).Value = DBNull.Value;
于 2013-05-14T07:10:07.907 回答
1

首先,您需要检查您的日期列是否接受空值。
如果没有,则允许 null 存在。

然后你可以使用

command.Parameters.AddWithValue("@param", DBNull.Value);

结合所有看起来像这样

 param[8] = new Service.SqlParameter();
 param[8].ParameterName = "@DOJ";
 param[8].Value = DOJ==null?DBNull.Value:DOJ;
 param[8].SqlDbType = Service.SqlDbType.DateTime;

你的财产应该是这样的

public DateTime? DOJ
{
    get;
    set;
}
于 2013-02-22T06:21:57.373 回答
1

您需要进行 3 处更改才能使您的代码正常工作,

更改 1:首先将您的 DateTime 变量设置为DateTime?

public DateTime? DOB { get; set; }
public DateTime? DOJ { get; set; }

更改 2:改用 DateTime.TryParse

DateTime DOB;
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
String datetime = txtDOB.Text.Trim();
DateTime.TryParse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out DOB);
if (DOB != DateTime.MinValue)
{
    objEmp.DOB = null; 
}
else
{
    objEmp.DOB = date;
}

为司法部做同样的事情。

更改 3:此外,当 datetime 的值为 null 时,请确保在将 DOB 的值分配给 sql 参数时进行 null 检查,如果 DOB 为 null 然后分配DBNull.Value

例如 :

param[7].ParameterName = "@DOB";
if(DOB == null)
{
    param[7].Value = DBNull.Value;
}
else
{
    param[7].Value = DOB;   
}
param[7].SqlDbType = Service.SqlDbType.DateTime;

是的,欢迎来到 Stackoverflow!

于 2013-02-22T06:22:07.083 回答
0

使您的属性可以为空

public DateTime? DOB
    {
        get;
        set;
    }
    public DateTime? DOJ
    {
        get;
        set;
    }

让你的代码像这样

IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
        String datetime = txtDOB.Text.Trim();

if(DateTime.TryParse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out date))
{
  objEmp.DOB = date;
}
于 2013-02-22T06:20:13.857 回答