以C#winform将图像读写到MS Access数据库

以C#winform将图像读写到MS Access数据库,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我应该建一个软件健身房。 我想给健身房的每位员工都附上一张照片。 因此,我遵循youtube上的优秀指南,该指南解释了如何将图像附加到数据。 问题是,当我附加图片时,图片附加在表中的新行中,而不是现有行中 谢谢 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using S

我应该建一个软件健身房。 我想给健身房的每位员工都附上一张照片。 因此,我遵循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();
        }


    }

}

如果要更新数据库中已经存在的内容,则在SQL查询中必须使用(id为旧记录)而不是。像这样:

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

或者作为变体(如果您想同时更新多个记录/多个字段,并简化插入/更新代码,这很好)-您可以使用“删除”删除旧记录,使用相同id的插入插入插入新记录,然后。

您是否考虑过,而不是观看youtube并认为这“非常好”也许-读一本书或阅读文档?我应该在两个月内提交项目。由于我不懂这门语言(C#),而且我应该独自学习所有东西,所以在youtube上有人做得很好!不仅仅是这样,还要问更多的问题。做更多的项目。Youtube视频有好有坏。。。它们可能涵盖一些非常基本的部分,但由于格式的原因,它们绝对无法解释深入技术内部的东西(关于REALY是如何工作的)——只有参考文档和源代码可以解释。是的,通过视频你可以得到工作结果。但你们很难得到这个问题的答案:“为什么这样工作,以及它如何以其他方式工作?”。但这在编程中是必要的。。MSDN,Technet,.NET参考源代码,Stackoverflow是程序员比youtube更好的朋友。:)