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.SqlClient;
using System.Configuration;
using System.IO;
namespace 照片存储
{
    public partial class Form1 : Form
    {
        Stream stream1;
        //FileStream fs;
        string conStr = ConfigurationManager.ConnectionStrings["studentManaConStr"].ToString();
        public Form1()
        {
            InitializeComponent();
        }
        private void Save_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conStr);
            string sql = "insert into studentPicture values(@studentId,@studPicture)";
            SqlCommand com = new SqlCommand(sql,con);
            string studentId = comboBox1.Text;
            int length = (int)stream1.Length;
            byte[] bytePic=new byte[length];
            stream1.Close();
            com.Parameters.Add("@studentId",SqlDbType.NVarChar).Value=studentId;
            com.Parameters.Add("@studPicture",SqlDbType.NVarChar).Value=bytePic;
            try
            {
                con.Open();
                com.ExecuteNonQuery();
                MessageBox.Show("保存成功!");
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "jpg文件|*.jpg|bmp文件|*.bmp|gif文件|*.gif";
             if (ofd.ShowDialog ()== DialogResult.OK)
             {
                 stream1 = ofd.OpenFile();
                 Image studentImage = Image.FromStream(stream1,true);
                 pictureBox1.Image = studentImage;
                 Save.Enabled = true;
             }
        }
        private void Read_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conStr);
            string sql="select studPicture from studentPicture where studentId=@studentId";
            SqlCommand com = new SqlCommand(sql,con);
            string studentId = comboBox1.Text;
            com.Parameters.Add("@studentId",SqlDbType.NChar).Value=studentId;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                dr.Read();
                MemoryStream ms = new MemoryStream(dr.GetSqlBinary(0).Value);
                Image image = Image.FromStream(ms, true);
                pictureBox1.Image = image;
                MessageBox.Show("读取成功!");
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            finally
            {
                con.Close();
            }
        }
                
           }
}
为什么在运行保存图片的时候,却出现异常:byte[]转化String时失败呢?请高手解答!!!

解决方案 »

  1.   

    com.Parameters.Add("@studPicture",SqlDbType.NVarChar).Value=bytePic;
    看看数据库是什么字段
    二进制应该是IMAGE字段吧
      

  2.   

    保存
    using(System.IO.FileStream stream = new System.IO.FileStreamfile,System.IO.FileMode.Open,System.IO.FileAccess.Read){
      byte[] buffer = new byte[stream.Length];  
      stream.Read(buffer, 0, (int)stream.Length);  
      stream.Close();  
      string strName = System.IO.Path.GetFileNameWithoutExtension(file);  
      SqlCommand cmd = new SqlCommand("", sqlConn);  
      cmd.Parameters.Add("@photo", SqlDbType.Image).Value = buffer;  
      cmd.ExecuteNonQuery();}
      

  3.   

    在数据库中图片存储字段为:image。在保存时为什么会出现byte[]转化为String时失败?而在读取图片时却没有出现问题?