ado存取文件的问题,我新手不太懂
如图,界面上有TADOConnection(名为con1),TADOQuery(名为qry1),TOpenDialog(名为dlgOPen),两个按钮(名为btn1,btn2)。
存入按钮打开一个文件对话框,选取一个文件存入数据库(ado)。
代码怎么实现?下面是我写的,但是插入不了文件。
//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"#include "ComObj.hpp"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender){ AnsiString path=ExtractFilePath(Application->ExeName); Variant CreateAccess; AnsiString s; if(!FileExists(Filename)) { s="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+path+"test.mdb"; CreateAccess=CreateOleObject("ADOX.Catalog"); CreateAccess.OleFunction("Create",s.c_str()); con1->ConnectionString=s; qry1->SQL->Add("create table mytable1(id int,\ nodeid int,itemcaption char(255),\ content binary,Primary Key(id))"); qry1->ExecSQL(); con1->Close(); } con1->ConnectionString=s;}//---------------------------------------void __fastcall TForm1::btn1Click(TObject *Sender){ if(!dlgOpen1->Execute()) return; qry1->Open(); qry1->Edit(); qry1->FieldByName("id")->AsInteger = 1; qry1->FieldByName("nodeid")->AsInteger = 0; qry1->FieldByName("itemcaption")->AsString = "我的文档"; TMemoryStream *tmpStream = new TMemoryStream(); TBlobField *tmpField; tmpStream->LoadFromFile(dlgOpen1->FileName); tmpField = (TBlobField *)qry1->FieldByName("content"); tmpField->LoadFromStream(tmpStream); qry1->Post(); tmpField = NULL; delete tmpStream;}//---------------------------------------
qry1->SQL->Add("create table mytable1(id int,\ nodeid int,itemcaption char(255),\ content OLEObject,Primary Key(id))");