从C#App在Access 2007 DB中插入和检索图像 [英] Insert and retrieve Image in Access 2007 DB from C# App
问题描述
我需要在具有OLE对象类型的列的访问数据库中插入图像 我尝试使用以下代码,但是在插入插入时给了我语法错误,但是我确定表名和列名以及imageContent值都不为空
I need to insert an image in access database that has a column of type OLE Object I tried to use the following code but it gives me a Syntax Error in insert staetment but I am sure of table name and column name and the imageContent value is not null
System.Data.OleDb.OleDbConnection MyConnection;
private void Insert_Customer_Load(object sender, EventArgs e)
{
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Customer.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123";
MyConnection = new System.Data.OleDb.OleDbConnection(strConn);
}
private void InsertBtn_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection.Open();
myCommand.Connection = MyConnection;
byte[] imageContent = File.ReadAllBytes(imagePathTxt.Text);
sql = "INSERT INTO [Customer_Data] (Image) VALUES (@photo)";
myCommand.CommandText = sql;
OleDbParameter ph = new OleDbParameter("@photo", OleDbType.VarBinary);
ph.Value = imageContent;
ph.Size = imageContent.Length;
myCommand.Parameters.Add(ph);
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
我也尝试使用?而不是@photo,但也会引发相同的异常.
Also I tried to use ? instead of @photo but also the same exception raised.
预先感谢
推荐答案
我认为Image
是保留字,所以也许只是您应该使用[Image]
,或者更好的是,重命名该列. (顺便说一句,您确定要在数据库中存储图像吗?)
I think Image
is a reserved word, so maybe it's just that you should use [Image]
or, better, rename the column. (BTW, are you sure you want to store images in your database?)
这篇关于从C#App在Access 2007 DB中插入和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!