将文件插入访问表

时间:2013-03-02 22:29:06

标签: sql ms-access

我有一个名为Reports的表,其中包含3个字段ID(自动编号),filename(字符串字段),theFile(附件字段)。

我想要的是运行SQL查询并将PDF文件插入附件字段(theFile)。

让我们说PDF文件位于C:驱动器(C:\ report1.pdf)中,我已经尝试了下面的SQL查询,但它无法正常工作。我知道将文件存储在数据库中并不是一种好的做法,但我只想尝试一下:

CurrentDb.Execute "INSERT INTO Reports (filename,theFile) VALUES ('report1'," & C:\report1.pdf & ")"

3 个答案:

答案 0 :(得分:2)

将文件存储在数据库中的标准做法。 Access肯定支持它,但不支持SQL。您必须使用DAO,详见http://msdn.microsoft.com/en-us/library/office/bb258184%28v=office.12%29.aspx

答案 1 :(得分:0)

“文件”不是Access available data types中支持的SQL数据类型。

答案 2 :(得分:0)

That is correct Derek, if you try to run a SQL statement like that you will get an error message of one type or another every time. I spent a fair amount of time researching this subject for my own DB, and from what I understand there are a number of options/alternatives; however, having an attachment column type and using SQL to insert a file is not an option with Access' current capabilities.

It is not bad practice to store files in a database, it is actually standard practice; however, it IS best practice to not store files in an ACCESS db. There are a few reasons for this which you can research on your own, but perhaps most notably, Access has a file size limit of 2GB, so if you store files in it you can run out of space quickly and then things get even more complicated.

Here are your options:

  1. Change your column data type to OLE object and use some kind of stream reader to convert the files to binary data, then use a SQL statement to load them into your DB
  2. Use the built in Access user interface for working directly with tables/attachments
  3. Establish a DAO db connection and use Access' recordset.LoadFromFile function
  4. Just store links to the files in the Access DB

The 4th option is the preferred method. It's very simple and you won't have to worry about complex code or the 2GB storage limit.