简体   繁体   English

通过创建datetime将null传递到sql server 2012 datetime的问题? 变量到我的存储过程

[英]Issue in passing null into sql server 2012 datetime by creating datetime? variable to my stored procedure

This is my business object layer 这是我的业务对象层

public class Servicetransactionbol
{
    public Servicetransactionbol()
    {
        st_id = default(int);
        st_startdate = default(DateTime);
        st_enddate = default(DateTime);
    }
       private int st_id;
    private DateTime st_startdate;
    private DateTime st_enddate;

     public int STid
    {
        get { return this.st_id; }
        set { this.st_id = value; }
    }

    public DateTime STstartdate
    {
        get { return this.st_startdate; }
        set { this.st_startdate = value; }
    }

    public DateTime STenddate
    {
        get { return this.st_enddate; }
        set { this.st_enddate = value; }
    }
}

This is my business access layer 这是我的业务访问层

public class servicetransactionbal
{
    servicetransactiondal servicetransactionDAL = new servicetransactiondal();

    public void insertTransaction(Servicetransactionbol servicetransactionBOL)
    {
        try
        {

            servicetransactionDAL.Inserttransaction(servicetransactionBOL);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
        }
    }
}

This is my data access layer 这是我的数据访问层

public class servicetransactiondal
{
   public void Inserttransaction(Servicetransactionbol servicetransactionBOL)
   {
       try
       {

           Database db = DatabaseFactory.CreateDatabase("GeniusStockTraders");
           DbCommand dbc = db.GetStoredProcCommand("Sproc_service_transaction");              
           db.AddOutParameter(dbc, "srvid", DbType.Int32, servicetransactionBOL.STid);
           db.AddInParameter(dbc, "srv_startdate", DbType.DateTime, servicetransactionBOL.STstartdate);
           db.AddInParameter(dbc, "srv_enddate", DbType.DateTime, servicetransactionBOL.STenddate);
           db.AddInParameter(dbc, "ttype", DbType.String, "insert_transaction");
           db.ExecuteNonQuery(dbc);
           dbc.Dispose();
       }
       catch (Exception ex)
       {
           throw ex;
       }
       finally
       {

       }

   }
 }

and finally this is my code behind 最后这是我背后的代码

    SqlDateTime? enddate;
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            Database db = DatabaseFactory.CreateDatabase("GeniusStockTraders");
            servicetransactionBOL.STid = default(int);
            servicetransactionBOL.STscript = txt_script.Text;
            servicetransactionBOL.STstartdate = Convert.ToDateTime(txt_startdate.Text);
            if (string.IsNullOrEmpty(txt_enddate.Text))
            {
                enddate = SqlDateTime.Null;
                servicetransactionBOL.STenddate = (DateTime)enddate;

            }
            else
            {
                servicetransactionBOL.STenddate = Convert.ToDateTime(txt_enddate.Text);
            }

With this code I am able to insert min value of datetime when my text field is empty but I want to insert NULL when my text field is empty. 使用此代码,我可以在我的文本字段为空时插入datetime的最小值,但是当我的文本字段为空时我想插入NULL。 I tried by making datetime as datetime? 我尝试将datetime作为datetime? in my business object layer then m able to insert NULL value to my enddate in LOCALHOST but when I upload same files online its throwing an exception 在我的业务对象层,然后能够在LOCALHOST中向我的enddate插入NULL值,但是当我在线上传相同的文件时,它会抛出异常

Method Not Found:'System.datetime GeniusstocktradersBOL.ServeicetransactionBOL.get_STenddate(). 找不到方法:'System.datetime GeniusstocktradersBOL.ServeicetransactionBOL.get_STenddate()。

Please help me to resolve this issue. 请帮我解决这个问题。

thank you 谢谢

A parameter with a Value of null : is not sent. 有一个参数Valuenull :不发送。 If you are getting issues about variables / parameters that aren't defined, you must instead replace null with DBNull.Value . 如果您遇到有关未定义的变量/参数的问题,则必须使用DBNull.Value替换null Yes, this is annoying. 是的,这很烦人。 For example: 例如:

DateTime? someValue = ... // could be null
db.AddInParameter(dbc, "some_name", DbType.DateTime,
    (object)someValue ?? DBNull.Value);

Note that in the example you provide, both date-times are DateTime , not DateTime? 请注意,在您提供的示例中,两个日期时间都是DateTime ,而不是DateTime? , but it applies in the more general case. ,但它适用于更一般的情况。

As for: 至于:

Method Not Found:'System.datetime GeniusstocktradersBOL.ServeicetransactionBOL.get_STenddate(). 找不到方法:'System.datetime GeniusstocktradersBOL.ServeicetransactionBOL.get_STenddate()。

That is nothing to do with null values, and simply suggests that you haven't fully built/deployed the dlls, ie that the server has a version of the dll without that property accessor . 这与null值无关,只是表明你没有完全构建/部署dll,即服务器有一个没有该属性访问器的dll版本。

use a case statement in the insert when Date=MIN('1/1/1900') then NULL , pass '1/1/1900' as the "Empty" value. when Date=MIN('1/1/1900') then NULL ,在插入中使用case语句,将'1/1/1900'作为“Empty”值传递。

Turning this into an answer as OP said it solved the problem. 将此转化为答案,因为OP说它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM