private void button2_Click(object sender, EventArgs e)//把要传的图片上传到数据库
{
//System.IO.FileStream fs2;
openFileDialog1.Filter = "(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.gif)|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation =openFileDialog1.FileName;
// FileStream fs2 = File.OpenRead(pictureBox1.ImageLocation);
FileStream fs2 = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs2.Length];//fs.lenth文件流长度,用字节表示
BinaryReader br = new BinaryReader(fs2);//二进制读写器
imagebytes = br.ReadBytes(Convert.ToInt32(fs2.Length));
// fs2.Read(imagebytes, 0, imagebytes.Length); SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES('imagebytes')", con);
cmd.ExecuteNonQuery(); MessageBox.Show("添加成功");
con.Close();
}
}
private void button1_Click(object sender, EventArgs e)//把图片从数据库中读出来
{ byte[] imagesbytes = null;//where CardID='"+ textBox1.Text.Trim() +"'
//string txt = null;
SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
con.Open();
SqlCommand com = new SqlCommand("Select Picture from Pic ", con);
SqlDataReader sdr = com.ExecuteReader();
//while (sdr.Read())
//{
// //if (sdr.GetValue(0) == null)
// // MessageBox.Show(sdr[0].ToString());
// //else
// imagesbytes = (byte[])sdr.GetValue(0);
// //MessageBox.Show(sdr.GetValue(0).ToString());
//}
if (sdr.Read())
{ byte[] photo = sdr[0] as byte[];//将第0列的数据写入byte数组 FileStream fs = new FileStream(pictureBox2.ImageLocation, FileMode.CreateNew);//创建FileStream对象,用于写入字节数据流 fs.Write(photo, 0, photo.Length);//将byte数组中的数据写入fs
Bitmap map = new Bitmap(fs);//
pictureBox2.Image = map; fs.Close();//关闭fs }
sdr.Close();
//MemoryStream ms = new MemoryStream(imagesbytes);
//Bitmap bmpt = new Bitmap(ms);
//pictureBox1.Image = bmpt; }
{
//System.IO.FileStream fs2;
openFileDialog1.Filter = "(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.gif)|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation =openFileDialog1.FileName;
// FileStream fs2 = File.OpenRead(pictureBox1.ImageLocation);
FileStream fs2 = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs2.Length];//fs.lenth文件流长度,用字节表示
BinaryReader br = new BinaryReader(fs2);//二进制读写器
imagebytes = br.ReadBytes(Convert.ToInt32(fs2.Length));
// fs2.Read(imagebytes, 0, imagebytes.Length); SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES('imagebytes')", con);
cmd.ExecuteNonQuery(); MessageBox.Show("添加成功");
con.Close();
}
}
private void button1_Click(object sender, EventArgs e)//把图片从数据库中读出来
{ byte[] imagesbytes = null;//where CardID='"+ textBox1.Text.Trim() +"'
//string txt = null;
SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database=library");
con.Open();
SqlCommand com = new SqlCommand("Select Picture from Pic ", con);
SqlDataReader sdr = com.ExecuteReader();
//while (sdr.Read())
//{
// //if (sdr.GetValue(0) == null)
// // MessageBox.Show(sdr[0].ToString());
// //else
// imagesbytes = (byte[])sdr.GetValue(0);
// //MessageBox.Show(sdr.GetValue(0).ToString());
//}
if (sdr.Read())
{ byte[] photo = sdr[0] as byte[];//将第0列的数据写入byte数组 FileStream fs = new FileStream(pictureBox2.ImageLocation, FileMode.CreateNew);//创建FileStream对象,用于写入字节数据流 fs.Write(photo, 0, photo.Length);//将byte数组中的数据写入fs
Bitmap map = new Bitmap(fs);//
pictureBox2.Image = map; fs.Close();//关闭fs }
sdr.Close();
//MemoryStream ms = new MemoryStream(imagesbytes);
//Bitmap bmpt = new Bitmap(ms);
//pictureBox1.Image = bmpt; }
解决方案 »
- C#对APP.config文件的操作
- ankhsvn插件,更改URL后如何更改源码连接
- 请问怎样编写一个控制台程序获得word中的内容?
- cookie传递的问题
- C#怎样获取数据库里某一列(数字int型)的最大值?[在线等]
- 请教关于复合自定义控件的事件含义
- C# 调com 组件。。。。。。。
- C#中類似VB中Format(now,"yy/mm/dd")或Format(num,"#,##0.00")的函數??
- 有没有API viewer for C#,快被调用API搞晕了,不想自己一个一个的转换了。
- 菜单与权限的问题!!!分不够可另开贴再加!!
- 求一个C#多线程的源码。越简单越好。要能够体现出多线程比单线程效率高
- 请教下自动更新问题 exe,dll文件报错404 文本和rar没问题
,你改成@传参方式
SqlCommand cmd = new SqlCommand("INSERT INTO Pic(Picture)VALUES(@imagebytes)", con);
你要是能把'imagebytes'这几个字符串转成Bitmap,那你就神了