zoukankan      html  css  js  c++  java
  • VS 2010中WinForm开发(C#)—图片上传到数据库与显示(sql server 2008)

    using System;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.IO;
    using System.Data.SqlClient;namespace photoSC
    {
    public partial class Form1 : Form
    {
    SqlConnection conn;
    public Form1()
    {
    InitializeComponent();
    }private void button1_Click(object sender, EventArgs e)
    {
    openFileDialog1.InitialDirectory = “C:\”;
    openFileDialog1.Filter = “图片文件 (*.jpg)|*.jpg”;
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.RestoreDirectory = true;
    openFileDialog1.Multiselect = true;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    textBox1.Text = openFileDialog1.FileName;
    }
    }
    private void button2_Click(object sender, EventArgs e)
    {
    Save(PhotoToArray(textBox1.Text.ToString()));
    Form1 f1 = new Form1();
    f1.ShowDialog();
    // 不在任务栏显示
    //this.ShowInTaskbar = false;
    // 窗体透明
    this.Opacity = 0; //后面的取值0~100 ,0表示透明
    
    }
    private byte[] PhotoToArray(string path)
    {
    FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
    byte[] bufferPhoto = new byte[stream.Length];
    stream.Read(bufferPhoto, 0, Convert.ToInt32(stream.Length));
    stream.Flush();
    stream.Close();
    return bufferPhoto;
    }
    private void Save(byte[] image)
    {
    conn = new SqlConnection(SQL.conStr);
    string sql = “insert into Photo(photo_Info,photo_Id) values(@photo,@photo_id)”;
    SqlParameter param = new SqlParameter();
    param = new SqlParameter(“@photo”, SqlDbType.Image);
    param.Value = image;
    SqlParameter param1 = new SqlParameter();
    param1 = new SqlParameter(“@photo_id”, SqlDbType.Int);
    param1.Value =textBox2.Text.ToString();
    SqlCommand commd = new SqlCommand(sql, conn);
    commd.Parameters.Add(param);
    commd.Parameters.Add(param1);
    try
    {
    conn.Open();
    commd.ExecuteNonQuery();
    MessageBox.Show(“您已经把图片成功的插入数据库!”);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    conn.Close();
    }
    }
    private void button3_Click(object sender, EventArgs e)
    {
    conn = new SqlConnection(SQL .conStr);
    string strSQL = “Select [photo_Info] From [Photo] Where [photo_Id]=(@photo)”;
    SqlParameter param = new SqlParameter();
    param = new SqlParameter(“@photo”, SqlDbType.Int);
    param.Value = comboBox1.Text.ToString();
    SqlCommand cmd = new SqlCommand(strSQL,conn);
    cmd.Parameters.Add(param);
    conn.Open();
    System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
    try
    {
    reader.Read();
    MemoryStream ms = new MemoryStream((byte[])reader["photo_Info"]);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    this.pictureBox1.Image = image;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    conn.Close();
    }
    
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
    // TODO: 这行代码将数据加载到表“exampelDataSet.Photo”中。您可以根据需要移动或删除它。
    this.photoTableAdapter.Fill(this.exampelDataSet.Photo);
    int photoid =Convert.ToInt32( comboBox1.Text.Trim().ToString());
    }
    }
    }
  • 相关阅读:
    Cesium 模拟卫星扫描
    SQL Server配置管理器”远程过程调用失败“的问题解决
    Windows系统查看端口占用、结束进程方法和命令
    Cesium 遥感卫星影像推送效果绘制
    Nginx 发布本地后台端口
    js 产生16位随机字符串
    vscode powershell/gitbash g++ : 无法将“g++”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1
    安装Tomcat服务器以及错误汇总(tomcat8.0、jdk8)
    Windows 8及以上系统安装好SQL Server 2008之后找不到SQL Server配置管理器的问题
    mysql 利用binlog增量备份,还原实例
  • 原文地址:https://www.cnblogs.com/lvk618/p/3338522.html
Copyright © 2011-2022 走看看