我需要在具有OLE对象类型的列的访问数据库中插入图像 我尝试使用以下代码,但它在插入staetment时给了我一个语法错误,但我确定表名和列名并且imageContent值不为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但也引发了相同的异常。
提前致谢
答案 0 :(得分:2)
我认为Image
是一个保留字,所以也许只是你应该使用[Image]
或更好地重命名该列。 (顺便说一句,你确定要将图像存储在数据库中吗?)
答案 1 :(得分:1)
重命名列 您应该使用[Image]