从 C# 应用程序在 Access 2007 DB 中插入和检索图像
我需要在 Access 数据库中插入一个具有 OLE 对象类型列的图像 我尝试使用以下代码,但它在插入语句中给了我一个语法错误,但我确定表名和列名,并且 imageContent 值不为空
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,但也引发了同样的异常。
提前致谢
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();
}
Also I tried to use ? instead of @photo but also the same exception raised.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
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?)重命名该列
你应该使用[图片]
rename the column
you should use [Image]