[英]“Syntax error in insert into statement” when inserting image into Access table
[英]Trying to save an image to Access: “Syntax error in INSERT INTO”
我希望有人可以幫我解決這個問題,我正在嘗試將圖片從圖片框保存到訪問數據庫中,我已將字段的數據類型設置為“ OLEDB”,並且正在使用以下代碼,但我收到了以下錯誤消息:在INSERT INTO字符串中有一個語法錯誤,除了可以將文本值和日期存儲在C#中的數據庫中之外,如果有人可以幫助的話,我是新手。
private void btnSave_Click(object sender, EventArgs e)
{
string strCon = Properties.Settings.Default.Database1ConnectionString;
Conn = new OleDbConnection(strCon);
DateTime dateLastSvc = Convert.ToDateTime(txtLastService.Text);
DateTime datePAT = Convert.ToDateTime(txtPatTest.Text);
DateTime dateSvcCal = Convert.ToDateTime(lblNextSvcCalc.Text);
MemoryStream ms = new MemoryStream();
pbxEquipmentImage.Image.Save(ms, ImageFormat.Png);
byte[] pbxAray = ms.GetBuffer();
Comm = new OleDbCommand();
Comm.CommandType = CommandType.Text;
Comm.CommandText = "INSERT INTO Equipment(SerialNumber, DeviceName, LotNo, Critical, ISO, ServiceInterval, LastService, PAT, Location, Supplier, ServiceProvider, Comments, NextService, Image, FilePath) VALUES (@Serial, @Device,@Lot, @Critical, @ISO, @SvcInt, @LastSvc, @PAT, @Location, @Supplier, @SvcProv, @Comments, @NextSvc, @photo, @Link)";
//Comm = new OleDbCommand(strSql, Conn);
//ms.Position = 0;
//ms.Read(pbxAray, 0, pbxAray.Length);
//pbxAray = ms.ToArray();
Comm.Parameters.AddWithValue("@Serial", txtSerialNumber.Text);
Comm.Parameters.AddWithValue("@Device", txtDeviceName.Text);
Comm.Parameters.AddWithValue("@Lot", txtLotNumber.Text);
Comm.Parameters.AddWithValue("@Critical", cbxCritical.Text);
Comm.Parameters.AddWithValue("@ISO", cbxISOcert.Text);
Comm.Parameters.AddWithValue("@SvcInt", txtServiceInterval.Text);
Comm.Parameters.AddWithValue("@LastSvc", dateLastSvc);
Comm.Parameters.AddWithValue("@PAT", datePAT);
Comm.Parameters.AddWithValue("@Location", cbxLocation.Text);
Comm.Parameters.AddWithValue("@Supplier", cbxSupplier.Text);
Comm.Parameters.AddWithValue("@SvcProv", cbxServiceProvider.Text);
Comm.Parameters.AddWithValue("@Comments", txtComments.Text);
Comm.Parameters.AddWithValue("@NextSvc", lblNextSvcCalc.Text);
Comm.Parameters.AddWithValue("@photo", pbxAray);
Comm.Parameters.AddWithValue("@Link", linkFilePath.Text);
Conn.Open();
try
{
Comm.ExecuteNonQuery();
MessageBox.Show("The record has been added to the database");
Conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show("Could not save the information due to: " + ex.Message);
Conn.Close();
}
finally
{
txtSerialNumber.Clear();
txtDeviceName.Clear();
txtLotNumber.Clear();
cbxCritical.Text = "Please Select";
cbxISOcert.Text = "Please Select";
txtServiceInterval.Clear();
txtLastService.Text = null;
txtPatTest.Text = null;
cbxLocation.Text = "Please Select";
cbxSupplier.Text = "Please Select";
cbxServiceProvider.Text = "Please Select";
txtComments.Clear();
lblNextSvcCalc.Text = null;
pbxEquipmentImage.Image = null;
linkFilePath.Text = null;
}
}
IMAGE
是Access SQL中的保留字 ,因此,為了引用表中的該字段,您需要將字段名稱括在方括號中。 也就是說,在您的CommandText
您需要替換
... NextService, Image, FilePath ...
同
... NextService, [Image], FilePath ...
。
ps-我想您也想替換
Comm.Parameters.AddWithValue("@NextSvc", lblNextSvcCalc.Text);
同
Comm.Parameters.AddWithValue("@NextSvc", dateSvcCal);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.