c# 将图片保存到access数据库中

c# 将图片保存到access数据库中

                string fullpath = E:\电脑图标\BMP图例\BestBMP48X48\2.bmp;//文件路径
                FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
                byte[] imagebytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                fs.Close();
                //打开数所
                OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+@"\经费分析数据库.mdb" + ";Persist Security Info=False");
                con1.Open();
                OleDbCommand cmd = con1.CreateCommand();
                OleDbTransaction trans = con1.BeginTransaction();
                cmd.Connection = con1;
                cmd.Transaction = trans;
                
                string strcomm = "insert into [单位人员信息]([照片])";
                strcomm += "values('" + imagebytes + "')";
                cmd.CommandText = strcomm;

                int intFalg = cmd.ExecuteNonQuery();
                trans.Commit();
                if (intFalg == 1)
                {
                    MessageBox.Show("保存图片成功");
                    pictureBox1.Image = null;
                }
                else
                {
                    MessageBox.Show("保存图片失败");
                    pictureBox1.Image = null;
                }
                con1.Close();

nathanma

leave a comment

Create Account



Log In Your Account