C#与sql进行图片存取

aacd20610659a4775ec1365f05398875.png

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

using System.Data.SqlClient;

namespace 图片存取

{

    public partial class 实例 : Form

    {

        public 实例()

        {

            InitializeComponent();

        }

        public static string str = "server=192.168.0.102;database=mysql;uid=sa;pwd=a123456";

        private void 实例_Load(object sender, EventArgs e)

        {

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="path"></param>

        public static void InsertImg(string path)

        {

            byte[] bytes = File.ReadAllBytes(path);

            SqlConnection con = new SqlConnection(str);

            con.Open();

            SqlCommand cmd = new SqlCommand("insert into table_2 values(@image)",con);

            cmd.Parameters.Add("@image", SqlDbType.Image).Value = bytes;

            cmd.ExecuteNonQuery();

            con.Close();

            //cmd.Dispose();

        }

        public static void ReadImg(string path)

        {

            SqlConnection con = new SqlConnection(str);

            con.Open();

            SqlCommand cmd = new SqlCommand("select image from table_2 ",con);

            object scalar = cmd.ExecuteScalar();

            byte[] bytes = (byte[])scalar;

            File.WriteAllBytes(path, bytes);

            con.Close();

            //cmd.Dispose();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            InsertImg(@"C: \Users\Administrator\Desktop\0811\3-2.jpg");

            ReadImg(@"C: \Users\Administrator\Desktop\0811\3-2-1.jpg");

        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值