我试图将图片从图片框保存到访问数据库 [英] Am trying to save image from picture box to access database

查看:82
本文介绍了我试图将图片从图片框保存到访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Whenever i try to save picture from picturebox to access database i usually get this error message  "An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll

Additional information: Path cannot be null."





这是我保存到数据库的代码



This is my code for saving to database

private void cmdSave_Click(object sender, EventArgs e)
       {

           int rowCount;
           rowCount = num.Next();
          // OpenFileDialog f1 = new OpenFileDialog();
         //  f1.ShowDialog();
          // picImageDisplay.ImageLocation = f1.FileName;

           //save the image to the database
           OleDbCommand cmd = new OleDbCommand();
           cmd.Connection = DBConnection;

           //creating a filestream and streamreader converting the pictures
           // to binary before saving to the database
           FileStream stream = null;
           StreamReader reader = null;

           stream = new FileStream(picPreview.ImageLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
           reader = new StreamReader(stream);

           byte[] fbyteArray = new byte[stream.Length];

           stream.Read(fbyteArray, 0, Convert.ToInt32(stream.Length));
           if(cboCoordinateLabel.Text=="Artery")
           {
               cmd.CommandText = "INSERT INTO artery VALUES(@image_id,@coordinate,@image)";
               cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = rowCount;
               cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
               cmd.Parameters.Add("@image", OleDbType.Binary, Convert.ToInt32(stream.Length)).Value = fbyteArray;

               DBConnection.Open();
           }

           else if(cboCoordinateLabel.Text=="Vein")
           {
               cmd.CommandText = "INSERT INTO veins VALUES(@id,coordinate,@image)";
               cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = rowCount;
               cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
               cmd.Parameters.Add("@image", OleDbType.Binary, Convert.ToInt32(stream.Length)).Value = fbyteArray;

               DBConnection.Open();
           }

           else
           {
               MessageBox.Show("Please select an option from the coordinate label to proceed");
           }

           try
           {
               cmd.ExecuteNonQuery();
               MessageBox.Show("Record has been saved");

           }

           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
               MessageBox.Show(ex.StackTrace.ToString());
           }
           finally
           {
               DBConnection.Close();
           }
       }

推荐答案

看看这个:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它适用于SQL Server而不是Access,但它与OleDBConnection和命令完全相同。
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's for SQL Server rather than Access but it's exactly the same process with OleDBConnection and command instead.


private void cmdSave_Click(object sender,EventArgs e)

{





arteryCount ++;

veinCount ++;









if(cboCoordinateLabel.Text!=)

{

if(cboCoordinateLabel.Text ==Artery)

{

尝试

{

//使用MemoryStream

MemoryStream ms = new MemoryStream();

pi cPreview.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] photo_aray = new byte [ms.Length];

ms。位置= 0;

ms。阅读(photo_aray,0,photo_aray.Length);



//保存数据

cmd.CommandText =插入动脉值(@ image_id,坐标,图像);

cmd.Parameters.Add(@ image_id,OleDbType.Integer).Value = arteryCount;

cmd.Parameters.Add(@ coordinate,OleDbType.VarChar).Value = coordinate;

cmd.Parameters.AddWithValue(@ image,photo_aray);

DBConnection.Open();

cmd.Connection = DBConnection;



cmd.ExecuteNonQuery();

MessageBox.Show(记录已经已保存);





}

catch(例外情况)

{

MessageBox.Show(ex.Message.ToString());

MessageBox.Show(ex.StackTrace.ToString());

}

终于

{

DBConnection.Close();

}

$ / $
}

其他

{

试试

{

//使用MemoryStream

MemoryStream ms = new MemoryStream();

picPreview.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg );

byte [] photo_aray = new byte [ms .Length];

ms.Position = 0;

ms。阅读(photo_aray,0,photo_aray.Length);



//保存数据

cmd.CommandText =插入静脉值(@ image_id,坐标,图像);

cmd.Parameters.Add( @image_id,OleDbType.Integer).Value = veinCount;

cmd.Parameters.Add(@ coordinate,OleDbType.VarChar).Value = coordinate;

cmd.Parameters.AddWithValue(@ image,photo_aray);



DBConnection.Open();

cmd.Connection = DBConnection;



cmd.ExecuteNonQuery();

MessageBox.Show(记录已保存);



}

catch(Excepti在前)

{

MessageBox.Show(ex.Message.ToString());

MessageBox.Show(ex.StackTrace.ToString ());

}

终于

{

DBConnection.Close();

}



}

}

//如果组合框是空的

else

{

MessageBox.Show(请从坐标标签中选择一个选项继续);

}



}
private void cmdSave_Click(object sender, EventArgs e)
{


arteryCount++;
veinCount++;




if(cboCoordinateLabel.Text != "")
{
if(cboCoordinateLabel.Text=="Artery")
{
try
{
//using MemoryStream
MemoryStream ms = new MemoryStream();
picPreview.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);

//save data
cmd.CommandText = "insert into artery values(@image_id,coordinate,image)";
cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = arteryCount;
cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
cmd.Parameters.AddWithValue("@image", photo_aray);
DBConnection.Open();
cmd.Connection = DBConnection;

cmd.ExecuteNonQuery();
MessageBox.Show("Record has been saved");


}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{
DBConnection.Close();
}

}
else
{
try
{
//using MemoryStream
MemoryStream ms = new MemoryStream();
picPreview.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);

//save data
cmd.CommandText = "insert into vein values(@image_id,coordinate,image)";
cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = veinCount;
cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
cmd.Parameters.AddWithValue("@image", photo_aray);

DBConnection.Open();
cmd.Connection = DBConnection;

cmd.ExecuteNonQuery();
MessageBox.Show("Record has been saved");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{
DBConnection.Close();
}

}
}
//if the combo box is empty
else
{
MessageBox.Show("Please select an option from the coordinate label to proceed");
}

}


这篇关于我试图将图片从图片框保存到访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆