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时失败呢?请高手解答!!!
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时失败呢?请高手解答!!!
解决方案 »
- Winform中的checkedListBox控件,某几个选项被选中的问题
- 安装VS.NET 2003 和2005 以及2008分别需要什么样的电脑配置呢?
- C#如何获取一个对象所定义的全部事件,并自动快速创建需要的事件处理过程及参数列表?
- API 高手请进 急!
- stream读取string
- 谁能给推荐一本C#控件开发的中文电子书,谢谢啦
- 大家帮忙看看这个黄页是什么原因
- 分页时出现的问题,帮我看看?滑入门者困惑
- 求救:共有三个表如何把两个表的部分字段内容添加到第三个表中去,具体如何实现
- 求救,江湖告急,哪位大侠救小弟于水火(SqlDataRead)
- (初学者)C#将信息添加到dataset有一行代码不太明白。。
- 关于ImageList 的问题
看看数据库是什么字段
二进制应该是IMAGE字段吧
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();}