我这样写了一个,但不知道怎样赋空值:
DBTIMESTAMP tmpTime;
tmpTime.Day = 0;
tmpTime.Month = 0;
tmpTime.Year = 0;
tmpTime.Hour = 0;
......objOLEDB.A = tmpTime;
objOLEDB.Insert();
但是这样写不行,该怎样设置这些值呢? -- 请教各位大侠!

解决方案 »

  1.   

    试一下objOLEDB.A = NULL
      

  2.   

    试一下不对objOLEDB.A赋值,赋值完其他字段后直接insert(前提是数据库允许)
      

  3.   

    应该行呀?结果什么样?
    字段A为NULL(从SQLSERVER看)是什么意思?
      

  4.   

    注意,上述方法,实际数据库中的值应该是NULL,但是在SQLSERVER中显示的可能是空白。
    如果你想把实际的值设为NULL,上述方法就可以,如果你想显示出来NULL,那你需要用case语句。
    语法如下:
    SELECT A,B,C
        CASE A
          WHEN NULL THEN 'NULL'
        END
    FROM 表名
      

  5.   

    DBTIMESTAMP strTemp;

    strTemp.day = 0;
    strTemp.month = 0;
    strTemp.year = 0;
    strTemp.hour = 0;
    strTemp.minute = 0;
    strTemp.second = 0;
    strTemp.fraction = 0; CdboLocationTest CTest; HRESULT hr = CTest.Open(); CTest.m_ID = intTemp;
    CTest.m_TimeTest = NULL;
    hr = CTest.Insert ();VC的代码是这样的,但是通不过,与SQLSERVER无关。
      

  6.   

    不对objOLEDB.A赋值,赋值完其他字段后直接insert也通不过?运行报错?
      

  7.   

    CTest.m_ID = intTemp;
    hr = CTest.Insert ();
    也报错?不可能吧?如果报错,就是数据库的问题。
      

  8.   

    报错呀,反正不能INSERT,但若正确设置strTemp(例:01/01/2001),就可以了。数据库也允许该字段为空呀,还有其他什么设置吗?
      

  9.   

    ?好像不太可能了,首先,在数据库的SQL试图中,键入INSERT INTO 表名 (ID) VALUES (123);
    如果该语句能执行则证明SQLSERVER没问题。
    在程序中,用Execute发出SQL命令,这样总可以把
      

  10.   

    DBTIMESTAMP strTemp;
    int intTemp;
    intTemp = 1;strTemp.day = 1;
    strTemp.month = 1;
    strTemp.year = 2001;
    strTemp.hour = 0;
    strTemp.minute = 0;
    strTemp.second = 0;
    strTemp.fraction = 0;CdboLocationTest CTest;HRESULT hr = CTest.Open();CTest.m_ID = intTemp;
    CTest.m_TimeTest = strTemp;
    hr = CTest.Insert ();
    这样是可以的,要是不设置m_TimeTest的值(数据库允许为NULL,该字段为DATETIME)要怎么办?
    数据库没问题的!
      

  11.   

    我是说,为什么一定要用CTest.Insert来完成呢?用CTest.Execute能不能解决问题?
      

  12.   

    因为连接数据库用的是OLEDB,用户要求必须这样操作数据库。你所说的CTest.Execute在OLEDB中可用吗?怎么用?
      

  13.   

    不给就算了,我再来说说ole db,class MyData
    {
    public:
    int m_ID;
    DATE m_Time;BEGIN_COLUMN_MAP(MyData)
    COLUMN_ENTRY_TYPE(1, DBTYPE_I4, m_ID)
    COLUMN_ENTRY_TYPE(2, DBTYPE_DATE, m_Time)
    END_COLUMN_MAP()
    };CDataSource ds;
    CSession    session;
    CCommand <CAccessor<MyData> > cust;然后,———— CDBPropSet dbinit(DBPROPSET_DBINIT);
    dbinit.AddProperty(DBPROP_AUTH_CACHE_AUTHINFO, true);
    dbinit.AddProperty(DBPROP_AUTH_ENCRYPT_PASSWORD, false);
    dbinit.AddProperty(DBPROP_AUTH_MASK_PASSWORD, false);
    dbinit.AddProperty(DBPROP_AUTH_PASSWORD, "");
    dbinit.AddProperty(DBPROP_AUTH_USERID, "Admin");
    dbinit.AddProperty(DBPROP_INIT_DATASOURCE, "D:\\test.mdb");
    dbinit.AddProperty(DBPROP_INIT_MODE, (long)16);
    dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
    dbinit.AddProperty(DBPROP_INIT_PROVIDERSTRING, "");
    dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
    dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false); HRESULT hr = ds.OpenWithServiceComponents("Microsoft.Jet.OLEDB.4.0", &dbinit);

    if(FAILED(hr))
            {
                AfxMessageBox("error!");
                return ;
            } session.Open(ds);
     
        CString strSQL= "SELECT * FROM Test";
    cust.Open(session, strSQL);在上面的代码中连接的是access数据库,我的机器里没装SQL SERVER,道理相同—— CString strSQL="INSERT INTO test (ID) VALUES (46)";

    cust.Close(); 
    cust.Open(session,strSQL,NULL,NULL,DBGUID_DBSQL,FALSE);
    cust.Close(); 
    这样就执行了一句无返回的SQL语句,进行了insert操作,并且datetime的字段为空您明白了吗???
      

  14.   

    thanks! my email address [email protected]
      

  15.   

    直接生成SQL语句,然后执行它,你可以任意控制
      

  16.   

    可做如下改动,不设置m_TimeTest的值试试class CdboLocationTestAccessor
    {
    public:
    LONG m_ID;BEGIN_COLUMN_MAP(CdboLocationTestAccessor)
    COLUMN_ENTRY(1, m_dwUserId)
    END_COLUMN_MAP()DEFINE_COMMAND(CdboLocationTestAccessor, _T("  SELECT  ID  FROM dbo.LocationTest")) // You may wish to call this function if you are inserting a record and wish to
    // initialize all the fields, if you are not going to explicitly set all of them.
    void ClearRecord()
    {
    memset(this, 0, sizeof(*this));
    }
    };
      

  17.   

    刚才出了点错
    可做如下改动,不设置m_TimeTest的值试试class CdboLocationTestAccessor
    {
    public:
    LONG m_ID;BEGIN_COLUMN_MAP(CdboLocationTestAccessor)
    COLUMN_ENTRY(1, m_ID)
    END_COLUMN_MAP()DEFINE_COMMAND(CdboLocationTestAccessor, _T("  SELECT  ID  FROM dbo.LocationTest")) // You may wish to call this function if you are inserting a record and wish to
    // initialize all the fields, if you are not going to explicitly set all of them.
    void ClearRecord()
    {
    memset(this, 0, sizeof(*this));
    }
    };
      

  18.   

    刚才出了点错
    可做如下改动,不设置m_TimeTest的值,再试试class CdboLocationTestAccessor
    {
    public:
    LONG m_ID;BEGIN_COLUMN_MAP(CdboLocationTestAccessor)
    COLUMN_ENTRY(1, m_ID)
    END_COLUMN_MAP()DEFINE_COMMAND(CdboLocationTestAccessor, _T("  SELECT  ID  FROM dbo.LocationTest")) // You may wish to call this function if you are inserting a record and wish to
    // initialize all the fields, if you are not going to explicitly set all of them.
    void ClearRecord()
    {
    memset(this, 0, sizeof(*this));
    }
    };
      

  19.   

    直接用SQL语句当然可以,不过我想dengyong的原本意思是想使用Insert函数(利用本地数据绑定),但是如何实现通知数据库Provider某一个字段的值为NULL值.
    这个需要另外一个OLEDB宏.BEGIN_COLUMN_MAP(MyData)
       COLUMN_ENTRY_TYPE(1, DBTYPE_I4, m_ID)
       COLUMN_ENTRY_TYPE(2, DBTYPE_DATE, m_Time)
       //Insert this
       COLUMN_ENTRY_STATUS(2, m_Time, m_timeStatus ) 
    END_COLUMN_MAP()
    然后,
    CdboLocationTest CTest;HRESULT hr = CTest.Open();CTest.m_ID = intTemp;/*
    通知Provider第2个Field的值为空.如果要取缺省值,设置m_timeStatus=DBSTATUS_S_DEFAULT即可
    */CTest.m_timeStatus = DBSTATUS_S_ISNULL;
    /*
    The value sent to the provider is NULL. The provider ignores the contents of the value and length parts of the consumer's buffer. 
    */hr = CTest.Insert ();
    ATLASSERT(hr == S_OK);
    另外,这个我也没有试过,理论应该如此.