尝试将图像保存到Access:“INSERT INTO中的语法错误”

时间:2014-09-16 09:16:07

标签: c# ms-access visual-studio-2012 ms-access-2010

我希望有人可以帮我这个我试图将图片从图片框保存到访问数据库,我已经将字段的数据类型设置为" 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;
        }
    }

1 个答案:

答案 0 :(得分:0)

IMAGE是Access SQL中的reserved word,因此要在表中引用该字段,您需要将字段名称括在方括号中。也就是说,在CommandText中,您需要替换

... NextService, Image, FilePath ...

... NextService, [Image], FilePath ...

P.S。 - 我想你也想替换

Comm.Parameters.AddWithValue("@NextSvc", lblNextSvcCalc.Text);

Comm.Parameters.AddWithValue("@NextSvc", dateSvcCal);