C# 将图像保存到access数据库中

C# 将图像保存到access数据库中,c#,ms-access-2007,C#,Ms Access 2007,我试图保存一个数字、日期时间和一个图像,以便使用c#应用程序访问数据库。 我编写了一个函数,将图像转换为base64string格式,然后使用该函数将图像作为字符串获取,然后保存。 然而,我得到一个错误,说“参数NULL异常未处理”。此错误发生在代码中的下一行 image.Save(流,image.RawFormat)* 我的代码如下: private void save_Click(object sender, System.EventArgs e) { string oneimg=I

我试图保存一个数字、日期时间和一个图像,以便使用c#应用程序访问数据库。 我编写了一个函数,将图像转换为base64string格式,然后使用该函数将图像作为字符串获取,然后保存。 然而,我得到一个错误,说“参数NULL异常未处理”。此错误发生在代码中的下一行 image.Save(流,image.RawFormat)*

我的代码如下:

private void save_Click(object sender, System.EventArgs e)
{
    string oneimg=ImageToBase64String(pictureBox1.Image);
    string twoimg=ImageToBase64String(pictureBox2.Image);
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Windows\roadsafety.accdb;Jet OLEDB:Database Password=sudeep;");
    con.Open();
    try
    {
    //    con.Open();

    OleDbCommand cmd = new OleDbCommand("insert into dashboard(id,dtime) values('" + textBox2.Text + "','" + DateTime.Now.ToString() + "','" + oneimg + "','" + twoimg + "')", con);
    cmd.ExecuteReader();
    MessageBox.Show("Succesfully saved");
    }

    catch (Exception k)
    {
        MessageBox.Show(k.ToString());
    }
}

private string ImageToBase64String(Image image)
{
    using (MemoryStream stream = new MemoryStream())
    {
        image.Save(stream, image.RawFormat);
        return Convert.ToBase64String(stream.ToArray());
    }
}
请帮忙

OleDbConnection myConnection = null;
try
{
   //save image to byte array and allocate enough memory for the image
   byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));

   //create the SQL statement to add the image data
   myConnection = new OleDbConnection(CONNECTION_STRING);
   OleDbCommand myCommand = new OleDbCommand("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', ?)", myConnection);
   OleDbParameter myParameter = new OleDbParameter("@Image", OleDbType.LongVarBinary, imagedata.Length);
   myParameter.Value = imagedata;
   myCommand.Parameters.Add(myParameter);

   //open the connection and execture the statement
   myConnection.Open();
   myCommand.ExecuteNonQuery();
}
finally
{
   myConnection.Close();
}
链接:


您确定
pictureBox1
pictureBox2
始终有一个与它们相关联的图像(即
pictureBox1.image
pictureBox2.image
永远不会为
null
)吗?请阅读本文: