C# byte[]如何写入数据库
来源:网络收集 点击: 时间:2024-03-01百度图库选一张图。

在Visual Studio2017新建:
一个窗体From1、两个Button命名Btn_ImgLoad (打开一张图)、Btn_Save(保存)一个picturebox命名为Img_box(显示一张图)和一个openFileDialog1控件,布局如下图

//添加需要的using
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

//设置变量名称
//变量名称
public static SqlConnection conn;
public static SqlCommand cmd ;
Stream ms;

写一个图片函数
//图片函数
private byte GetImageBytes(Image image)
{
Bitmap bmp = new Bitmap(image);
MemoryStream mstream = new MemoryStream();
bmp.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
mstream.Seek(0, SeekOrigin.Begin); //及时定位流的开始位置
byte byteData = new Byte;
mstream.Position = 0;
mstream.Read(byteData, 0, byteData.Length);
mstream.Close();
return byteData;
}

//打开一张图片保存到picturebox中
private void Btn_ImgLoad_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = *.jpg|*.jpg|*.bmp|*.bmp;//指定openFileDialog控件打开的文件格式
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = *jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
if ((ms = openFileDialog1.OpenFile()) != null)
{
//获取当前选择的图片
this.Img_Box.Image = Image.FromStream(this.openFileDialog1.OpenFile());
//获取当前图片的路径
string path = openFileDialog1.FileName.ToString();
//将制定路径的图片添加到FileStream类中
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
//通过FileStream对象实例化BinaryReader对象
BinaryReader br = new BinaryReader(fs);
//通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组
byte imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));
}
else
{
MessageBox.Show(您选择的图片不能被读取或文件类型错误!, 错误 , MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}


//启动时设置数据库信息
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection();
conn.ConnectionString = @Data Source=你的数据库实例名称;Initial Catalog=BDProjectt2020;
Integrated Security=SSPI;MultipleActiveResultSets=true;Connection Timeout=90;
// 如果数据库关闭,则打开数据库
if (conn.State == ConnectionState.Closed)
try
{
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(打开数据库失败,请检查数据设置. + ex.Message, 消息提示, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
}

图片保存到数据库对应数据表事件,应根据自己的数据表字段写SQL语句
//图片保存事件
private void Btn_Save_Click(object sender, EventArgs e)
{
byte imageBytes = GetImageBytes(Img_Box.Image);
string SqlStr = INSERT INTO DBImg(ID,Name,Img)VALUES(1,测试图片,@ImgData);
SqlCommand cmd = new SqlCommand(SqlStr, conn);
SqlParameter param = new SqlParameter(ImgData, SqlDbType.VarBinary, imageBytes.Length);
param.Value = imageBytes;
// 如果数据库关闭,则打开数据库
if (conn.State == ConnectionState.Closed)
try{conn.Open();}
catch (Exception ex)
{MessageBox.Show(打开数据库失败,请检查数据设置. + ex.Message, 消息提示, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;}
cmd.Parameters.Add(param);
int i = cmd.ExecuteNonQuery();
MessageBox.Show(i + 条图片数据保存成功);
}

byte数据成功写入数据库中,可在数据表里查看到被保存的图片信息。


【1】自己或到百度图库准备一张图片。
【2】创建程序窗体和所需控件。
【3】添加所需using命名空间。
【4】设置变量名称。
【5】写一个图片处理函数方法
【6】创建选定图片事件。
【7】设置数据库相关信息。
【8】创建保存事件把图片保存到数据库中。
注意事项根据实际数据设置相应信息。
根据实际数据表字段保存。
图片存数据库图片与SQL版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_205470.html