在c# winform中读写图像到MS Access数据库

本文关键字:MS Access 数据库 图像 读写 winform | 更新日期: 2023-09-27 17:50:32

我应该建一个软件健身房。我想附上一张照片,每个员工在健身房。所以我跟随youtube上的优秀指南,解释了如何将图像附加到数据上。问题是,当我附加图片时,附加的是表中的新行,而不是现有的

谢谢! !

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 gym
{
    public partial class Load_Save : Form
    {
        OleDbConnection DBConnection = new OleDbConnection();
        OleDbDataAdapter DataAdapter;
        DataTable LocalDataTable = new DataTable();
        int rowPosition=0;
        int rowNumber=0;

        public Load_Save()
        {
            InitializeComponent();
        }
        private void Load_Save_Load(object sender, EventArgs e)
        {
              ConnectToDatabase();  
        }
        private void ConnectToDatabase()
        {
            DBConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Directory.GetCurrentDirectory() + "''data.mdb";  
            DBConnection.Open();
            DataAdapter = new OleDbDataAdapter("Select * From Workers", DBConnection);
            DataAdapter.Fill(LocalDataTable);
            if(LocalDataTable.Rows.Count !=0)
            {
                rowPosition = LocalDataTable.Rows.Count;
            }
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                btnSave.Enabled = true;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            StoreData(ConvertImageToBytes(pictureBox1.Image));
        }
        private byte[] ConvertImageToBytes(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;
        }
        private void StoreData(byte[] ImageAsBytes)
        {
            if(DBConnection.State.Equals(ConnectionState.Closed))
               DBConnection.Open();

            try
            {
                MessageBox.Show("Saving image at index: " + rowPosition.ToString());
                OleDbCommand oledbInsert = new OleDbCommand("Insert INTO Workers (id,firtsname,lastname,email,username,job,passw,permission,phone,dateofjoin,photo) VALUES('" + "','" + "','" + "','" + "','" + "','" + "','" + "','" + "','" + "','" + "',@Myphoto)", DBConnection);
                OleDbParameter imageParameter = oledbInsert.Parameters.AddWithValue("@photo", SqlDbType.Binary);
                imageParameter.Value = ImageAsBytes;
                imageParameter.Size = ImageAsBytes.Length;
                int rowsAffected = oledbInsert.ExecuteNonQuery();
                MessageBox.Show("data stored succ in" + rowsAffected.ToString() + " ROW");
                rowPosition++;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                MessageBox.Show(ex.StackTrace.ToString());
            }
            finally 
            {
                RefreshDBCconnection();
            }
        }

        private void RefreshDBCconnection()
        {
            DBConnection.Close();
            LocalDataTable.Clear();
            ConnectToDatabase();
        }

    }

}

在c# winform中读写图像到MS Access数据库

如果你想更新数据库中的一些东西,一些已经存在的东西,那么在sql查询中你必须使用update语句(旧记录的id)而不是INSERT。像这样:

UPDATE Workers SET photo=@photo WHERE id=@old_id

或者作为变体(如果你想同时更新多个记录/多个字段,并简化插入/更新代码)-你可以开始交易,用"delete"删除旧记录,使用相同id的insert插入新记录,然后COMMIT TRANSACTION