[英]Saving image into access database
我正在嘗試使用 c# 應用程序保存數字、日期時間和圖像以訪問數據庫。 我編寫了一個將圖像轉換為 base64string 格式的函數,然后我使用該函數將圖像作為字符串獲取,然后將其保存。 但是,我收到一條錯誤消息,指出“未處理參數 NULL 異常”。 此錯誤發生在代碼 image.Save(stream, 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();
}
鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.