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());
    }
    }
    }
  • 相关阅读:
    团队项目冲刺七
    团队项目冲刺六
    团队项目冲刺5
    团队项目冲刺4
    团队项目测试计划
    团队项目冲刺第三天进度和遇到问题
    团队项目冲刺第二天进度和问题
    博客园用户体验
    团队项目冲刺第一天进度和问题
    团队项目风险
  • 原文地址:https://www.cnblogs.com/lvk618/p/3338522.html
Copyright © 2011-2022 走看看