这么简单的任务:如何在Access 2010中存储Byte []?(整天都在网上搜索此内容。)
我必须在访问2010中使用“附件字段”,因为据我所知,没有其他可能的(varBinary,Image,..)字段可用。
我试过:(ConvertImageToByte返回Byte [])
Cmd.CommandText = "UPDATE Clubs SET Field1 = @File WHERE Name = @Name";
OleDbParameter para = new OleDbParameter("@File", OleDbType.VarBinary);
para.Value = ConvertImageToByte(Logo);
Cmd.ExecuteNonQuery();
异常:“UPDATE或DELETE查询不能包含多值字段。”
我试过了:
DBEngine dbe = new DBEngine();
Database db = dbe.OpenDatabase("database.accdb", false, false, "");
String Command = "SELECT * FROM Clubs";
Recordset rs = db.OpenRecordset(Command, RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);
rs.MoveFirst();
rs.Edit();
Recordset2 rs2 = (Recordset2)rs.Fields["Field1"].Value;
rs2.AddNew();
Field2 f2 = (Field2)rs2.Fields["FileData"];
f2.LoadFromFile("file.png");
rs2._30_Update();
rs2.Close();
rs._30_Update();
rs.Close();
这有效,但文件始终位于表格的第一行,我无法弄清楚如何获得正确的行。如果我尝试在SELECT语句中添加WHERE子句,则得到“参数太少。预期2”。异常。
如果有人知道如何将我的Byte [](或图像)发布到数据库中,请再次将其告知我们!
请不要给我链接:
http://www.mikesdotnetting.com/Article/123/Storing-Files-and-Images-in-Access-with-ASP.NET
http://www.sitepoint.com/forums/showthread.php?t=666928
Programmatically managing Microsoft Access Attachment-typed field with .NET
感谢您的帮助。
答案 0 :(得分:1)
您可以使用OLE对象字段,它是varbinary(max)匹配的最佳选择。
一些注意事项:
''Reference: Microsoft ActiveX Data Object x.x Library
Dim strSQL As String
Dim strCN As String
Dim rs As dao.Recordset
Dim mstream As ADODB.Stream
strSQL = "SELECT Pix FROM Table1"
Set rs = CurrentDb.OpenRecordset(strSQL)
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile "c:\docs\project.jpg" ''FileName & FullPath
rs.AddNew
rs.Fields("Pix").Value = mstream.Read
rs.Update
rs.Close
修改
要复制回磁盘,您可以再次使用Stream:
Dim strSQL As String
Dim cn As New ADODB.Connection
Dim mstream As New ADODB.Stream
strSQL = "SELECT Pix FROM Table1"
Set rs = CurrentDb.OpenRecordset(strSQL)
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs!Pix
mstream.SaveToFile "c:\docs\pixout.jpg", adSaveCreateOverWrite