基本信息
源码名称:将图片保存到数据表当中
源码大小:0.04M
文件格式:.7z
开发语言:C#
更新时间:2015-08-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍

C#示例,将图片保存到数据库当中。


 

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.Xml;
using System.IO;
using System.Data.SqlClient;

namespace SavePic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        PublicClass pubs = new PublicClass();

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fullpath = openFileDialog1.FileName;
                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));
                //把所选图片文件的流中的数据读入字节数组 


                SqlConnection conn = new SqlConnection("data source=10.114.48.57;Initial Catalog=eweaver;User ID=oa;Password=OAoa123"); 
                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into uf_user_paths(USERID,USERNAME,U_PATHS) values(@USERID,@USERNAME,@U_PATHS)", conn);
                cmd.Parameters.Add("@USERID", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@U_PATHS", SqlDbType.Image);//与ACCESS数据库唯一不同点 

                cmd.Parameters["@USERID"].Value = textBox1.Text;
                cmd.Parameters["@USERNAME"].Value = textBox2.Text; 
                cmd.Parameters["@U_PATHS"].Value = imagebytes; 
                cmd.ExecuteNonQuery(); 
                conn.Close(); 


                MessageBox.Show("信息保存成功!"); 
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowDatas();
        }

        private void ShowDatas()
        {
            string strSql = "select ID as 编号,USERID as 工号,USERNAME as 姓名 from uf_user_paths order by ID";
            BaseDatas db = new BaseDatas();
            DataSet ds = db.GetDataSet(strSql, pubs.GetStrConn());
            label3.Text = "查询结果:"   ds.Tables[0].Rows.Count.ToString()   " 条数据信息";
            dgv_infos.DataSource = ds.Tables[0];
            dgv_infos.Refresh();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fullpath = openFileDialog1.FileName;
                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));
                //把所选图片文件的流中的数据读入字节数组 


                SqlConnection conn = new SqlConnection("data source=10.114.48.57;Initial Catalog=eweaver;User ID=oa;Password=OAoa123");
                conn.Open();
                SqlCommand cmd = new SqlCommand("update uf_user_paths set U_PATHS=@U_PATHS where USERID=@USERID", conn);
                cmd.Parameters.Add("@USERID", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@U_PATHS", SqlDbType.Image);//与ACCESS数据库唯一不同点 

                cmd.Parameters["@USERID"].Value = textBox1.Text;
                cmd.Parameters["@U_PATHS"].Value = imagebytes;
                cmd.ExecuteNonQuery();
                conn.Close();
                ShowDatas();
                MessageBox.Show("信息修改成功!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {


            SqlConnection conn = new SqlConnection("data source=10.114.48.57;Initial Catalog=eweaver;User ID=oa;Password=OAoa123");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from uf_user_paths where ID='"  textBox3.Text  "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            ShowDatas();
            MessageBox.Show("信息删除成功!");
        }

    }
}