在C#中从Access数据库存储和检索图像 [英] Storing and retrieving images from an Access Database in C#
问题描述
我不是C#编程的新手,正在尝试创建一种软件,该软件将允许用户将信息和图片保存到Ms访问数据库中.我已经按照youtube上的教程进行操作,并设法存储了所需的信息和图片,但是我似乎无法检索图片并将其显示在图片框中.有人可以帮我吗?
Im new at C# programming and am trying to create a software that will allow the user to save information and a picture into a Ms access database. I have followed a tutorial on youtube and managed to store the info I needed and the picture but I cant seem to retrieve the image and display it in a picture box. can anyone help me?
我的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace ConnectionApp
{
public partial class ClinicaPrivada : Form
{
OleDbConnection DBConnection = new OleDbConnection();
OleDbDataAdapter DataAdapter;
DataTable LocalDatatable = new DataTable();
int rowPosition = 0;
int rowNumber = 0;
public ClinicaPrivada()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ConnectToDatabase();
}
public void ConnectToDatabase()
{
DBConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ClinicaPrivada.mdb";
DataAdapter = new OleDbDataAdapter("Select * From Pacientes", DBConnection);
DataAdapter.Fill(LocalDatatable);
if (LocalDatatable.Rows.Count != 0)
{
rowPosition = LocalDatatable.Rows.Count;
}
}
public void RefreshDBConnection()
{
if (DBConnection.State.Equals(ConnectionState.Open))
{
DBConnection.Close();
LocalDatatable.Clear();
ConnectToDatabase();
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
}
public byte[] ConvertAndStoreToDB(Image InputImage)
{
Bitmap BmpImage = new Bitmap(InputImage);
MemoryStream Mystream = new MemoryStream();
BmpImage.Save(Mystream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] ImageAsBytes = Mystream.ToArray();
return ImageAsBytes;
}
public void StoreToDB(byte[] ImageAsBytes)
{
string NrDoPaciente;
NrDoPaciente = Convert.ToString(txtNrdoPaciente.Text);
string Empresa;
Empresa = Convert.ToString(txtEmpresa.Text);
string DataDaConsutla;
DataDaConsutla = Convert.ToString(txtDatadaConsulta.Text);
if (DBConnection.State.Equals(ConnectionState.Closed))
DBConnection.Open();
try
{
MessageBox.Show("Saving Image at index:" + rowPosition.ToString());
OleDbCommand insert = new OleDbCommand("Insert Into Pacientes (NrDoPaciente, Empresa, DatadaConsulta, Document) VALUES('" + NrDoPaciente +"','" + Empresa + "','" + DataDaConsutla + "','" + "Document" + "')" , DBConnection);
OleDbParameter InsertParameter = insert.Parameters.AddWithValue("Docu", SqlDbType.Binary);
int rowsAffected = insert.ExecuteNonQuery();
MessageBox.Show("Data Stored successfully in " + rowsAffected.ToString() + "Row");
rowPosition++;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{
RefreshDBConnection();
}
}
private Image ReadImageFromDB()
{
Image FetchedImg;
if (rowNumber >= 0)
{
byte[] FetchedImgBytes = (byte[])LocalDatatable.Rows[rowNumber]["Document"];
MemoryStream stream = new MemoryStream(FetchedImgBytes);
FetchedImg = Image.FromStream(stream);
return FetchedImg;
}
else
{
MessageBox.Show("There are no Images in the database yet. Please reconnect or add some images.");
return null;
}
}
private void btnBrowse_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void btnSave_Click_1(object sender, EventArgs e)
{
StoreToDB(ConvertAndStoreToDB(pictureBox1.Image));
}
private void btnDisplayAll_Click(object sender, EventArgs e)
{
RefreshDBConnection();
rowNumber = 0;
pictureBox2.Image = ReadImageFromDB();
btnNext.Enabled = true;
BtnPrevious.Enabled = true;
}
}
}
推荐答案
您的内存中的图像可能超出范围. 这个问题,如果我是正确的,则直接相关.
Your image in memory is likely going out of scope. This question is directly related, if I am correct.
您必须对图像进行复制.否则,您的图像将链接到创建该图像的对象,并且当该对象超出范围时,图像数据将丢失.
You must make a copy of the image. Otherwise, your image is linked to the object that created it and when that object goes out of scope, your image data is lost.
我希望您会从尝试中获得例外.
I would expect you are getting exceptions from what you are trying to do.
顺便说一句,我同意建议避免使用MS Access的评论者.有很多更好的免费选项可供您选择.
BTW, I agree with the commenter who suggested avoiding MS Access. There are much better free options available to you.
这篇关于在C#中从Access数据库存储和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!