晚上的最后一个问题。并为这个完全的菜鸟道歉。
在stackoverflow和google的帮助下,我实现了以下...
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ADOCommand1->CommandText = "drop table pictures purge";
ADOCommand1->Execute();
ADOCommand1->CommandText = "create table pictures(id autoincrement, filename TEXT(255), notes text(255), category text(20), rank int, filedate datetime,constraint table1_PK primary key(id));";
ADOCommand1->Execute();
AddFiles(Edit1->Text);
}
//---------------------------------------------------------------------------
int AddFiles(AnsiString path, int count)
{
TSearchRec sr;
int f,count2=0;
f = FindFirst(path+"\\*.*", faAnyFile, sr);
while( !f )
{
if(sr.Attr & faDirectory)
{
if(sr.Name != "." && sr.Name != "..")
{
AnsiString subpath;
subpath.sprintf("%s%s%s", path, "\\", sr.Name);
count = AddFiles(subpath,count);
}
}
else
{
Form1->ADOCommand1->CommandText = "Insert into pictures (filename) values ('" + StringReplace(path + "\\" + sr.Name, "'", "''", TReplaceFlags()<<rfReplaceAll) + "')";
Form1->ADOCommand1->Execute();
++count;
++count2;
if (count2 > 100)
{
count2 = 0;
Form1->Caption = "Added " + IntToStr(count)+ " Files.";
Application->ProcessMessages();
}
}
f = FindNext(sr);
}
FindClose(sr);
return count;
}
它基本上是遍历给定目录及其子目录,并将每个文件(带有路径)添加到 MS Access 数据库中。
(我是唯一的用户,所以我不太关心安全性)
对于大量文件,此代码非常慢。谁能描述一个更好的方法来做代码正在做的事情,(希望不要让你的答案太复杂。我是这个记住的菜鸟)