广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    C# byte[]如何写入数据库

    来源:网络收集  点击:  时间:2024-03-01
    【导读】:
    C# 制作软件,将一张图片转换成二进制保存数据库中,来实现byte数据写入数据库。工具/原料more开发工具:Visual Studio Professional 2017 SqlServer版本:SQL Server 2014 Management Studio电脑:笔记本系统:win10家庭版方法/步骤1/9分步阅读

    百度图库选一张图。

    2/9

    在Visual Studio2017新建:

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

    3/9

    //添加需要的using

    using System;

    using System.Data;

    using System.Drawing;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    using System.IO;

    4/9

    //设置变量名称

    //变量名称

    public static SqlConnection conn;

    public static SqlCommand cmd ;

    Stream ms;

    5/9

    写一个图片函数

    //图片函数

    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;

    }

    6/9

    //打开一张图片保存到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);

    }

    }

    }

    7/9

    //启动时设置数据库信息

    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;

    }

    }

    8/9

    图片保存到数据库对应数据表事件,应根据自己的数据表字段写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 + 条图片数据保存成功);

    }

    9/9

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

    总结1/1

    【1】自己或到百度图库准备一张图片。

    【2】创建程序窗体和所需控件。

    【3】添加所需using命名空间。

    【4】设置变量名称。

    【5】写一个图片处理函数方法

    【6】创建选定图片事件。

    【7】设置数据库相关信息。

    【8】创建保存事件把图片保存到数据库中。

    注意事项

    根据实际数据设置相应信息。

    根据实际数据表字段保存。

    图片存数据库图片与SQL
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_205470.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-07 05:16:40  耗时:0.028
    0.0281s