无法将文件插入变体中 [英] Can not insert file into variant

查看:87
本文介绍了无法将文件插入变体中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件插入到MS Access数据库中,进入OLE对象类型的字段。我正在使用C ++和ADO。



我认为我的问题是错误处理变种,因为这是我第一次使用它们。我正在学习此代码示例 [ ^ ]但是在理解如何将文件从磁盘插入变体方面存在问题。



他们从数据库中读取它,并将其复制到新记录中,以便部分在哪里我从磁盘读取文件,然后将其插入变量中丢失。



当选择菜单项时,我在GUI中解雇我的代码。数据库有一个名为 test 的表,其中字段为 ID ,这是主键,字段哪个是 OLE对象类型。



在线搜索后我发现什么都没有帮助我。



这是最小的代码片段,可以说明问题(错误检查很少):



I am trying to insert a file into MS Access database, into a field of OLE Object type. I am using C++ and ADO.

I think that my problem is mishandling variants since this is the first time I use them. I am learning from this code example[^] but have problem understanding how to insert file from disk into variant.

They read it from database, and copied it into new record so the part where I read file from disk and then insert it into variant is missing.

I am firing off my code in GUI when menu item is selected. Database has one table named test with fields ID which is primary key and field which is of OLE Object type.

After searching online I have found nothing that can help me.

Here is smallest code snippet possible that illustrates the problem ( error checking is minimal):

wchar_t *bstrConnect = L"Provider=Microsoft.ACE.OLEDB.12.0; \
        Data Source = C:\\Users\\Smiljkovic85\\Desktop\\OLE.accdb";

try
{
    HRESULT hr = CoInitialize(NULL);

    // connection
    ADODB::_ConnectionPtr pConn(L"ADODB.Connection");

    // recordset
    ADODB::_RecordsetPtr pRS(L"ADODB.Recordset");

    // connect to DB
    hr = pConn->Open(bstrConnect, L"admin", L"", ADODB::adConnectUnspecified);

    // open file
    std::ifstream in(L"C:\\Users\\Smiljkovic85\\Desktop\\file.pdf",
           std::ios::ate | std::ios::binary);

    // get file size
    int fileSize = in.tellg();

    // here I tried to addapt code from the example linked above
    pRS->Open(L"test", _variant_t((IDispatch*)pConn, true), 
        ADODB::adOpenKeyset, ADODB::adLockOptimistic, ADODB::adCmdTable);

    // add new record
    pRS->AddNew();

    _variant_t varChunk;

    SAFEARRAY FAR *psa;
    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound = 0;
    // modify to our file size
    rgsabound[0].cElements = fileSize;
    psa = SafeArrayCreate(VT_UI1, 1, rgsabound);

    //=================== try to add file into variant
    char *chData = (char *)psa->pvData;
    chData = new char[fileSize];
    in.read(chData, fileSize);

    //=================================
    // Assign the Safe array  to a variant. 
    varChunk.vt = VT_ARRAY | VT_UI1;
    varChunk.parray = psa;

    pRS->Fields->GetItem(L"field")->AppendChunk(varChunk);
    // add this record into DB
    pRS->Update();

    // cleanup
    delete[] chData;
    in.close();
    pRS->Close();
    pConn->Close();
    CoUninitialize();
}
catch (_com_error e)
{
    MessageBox(hWnd, (LPWSTR)e.Description(), L"", 0);
}





你能帮我修改这段代码片段,以便将文件插入变种吗?



Can you help me to modify this code snippet so I can insert file into variant?

推荐答案

您不应该将文件插入数据库。保存文件的最佳做法是为它们提供一个唯一的名称(类似UUID)并将它们存储在db-server上的某个特殊文件夹中。您还必须保存原始文件名。



您的代码中的错误是使用VT_UI1并且大小为1.

You should NOT insert a file into the db. Best practice ins saving files is to give them a unique name (UUID-like) and store them in a special folder, somewhere on the db-server. You must also save the original file name.

The error in your code is on the use of VT_UI1 and the size of 1.
SafeArrayCreate(VT_UI1, fileSize, rgsabound);


这篇关于无法将文件插入变体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆