添加图片到SQLSERVER问题! 哪位大哥能不能给段添加图片到数据库的代码啊!小弟有急用!谢谢我用的是ASP.NET+C#+SQLSERVER万分感谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //把图片读到pictureBox,再写入数据库SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");conn.Open();SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);byte[] ib=new Byte[60000];FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );fs.Read(ib,0,60000);cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);cmd.Parameters["@i"].Value=ib;cmd.ExecuteNonQuery();conn.Close();-------------------------------------------------------//从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");conn.Open();SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);SqlDataReader reader=cmd.ExecuteReader();reader.Read();MemoryStream buf=new MemoryStream((byte[])reader[0]);Image image=Image.FromStream(buf,true);pictureBox1.Image=image; 保存图片 FileStream fs=File.OpenRead(txtfile.Text); byte[] content=new byte[fs.Length]; fs.Read(content, 0,content.Length); fs.Close(); conn.Open(); string sql ="insert into Photos(name,Photo) values(@name, @Photos)"; SqlCommand comm=new SqlCommand(sql,conn); comm.Parameters.Add("@Photos", SqlDbType.Image).Value=content; comm.Parameters.Add("@name", SqlDbType.NVarChar).Value=txtname.Text; if(comm.ExecuteNonQuery()==1) { MessageBox.Show("Successfully insert image into database!"); } else { MessageBox.Show("Failed to insert image into database"); } conn.Close();读取图片: private void button1_Click(object sender, System.EventArgs e) { ShowImage(txtname.Text); } private void ShowImage(string s) { string str = "SELECT photo FROM Photos WHERE name='" + s +"'"; SqlCommand cmd = new SqlCommand(str, conn); conn.Open(); byte[] b= (byte[])cmd.ExecuteScalar(); if (b.Length > 0) { MemoryStream stream = new MemoryStream(b, true); stream.Write(b, 0, b.Length); DrawToScale(new Bitmap(stream)); stream.Close(); } conn.Close(); } private void DrawToScale(Image bmp) { pictureBox1.Image = new Bitmap(bmp); }这个是WinForm的实现方法,在WEBFORM下应该也差不多了, 解释下这段代码各行的意思吧,小弟看不懂 这儿表达式改怎么写? 如何获取博客日志的内容 javascript字符串转化16进制数字 关于from表单提交到iframe的问题 博客园的DUDU是一个什么样的人?他是不是博客园的老板? 求两个正则表达式 菜鸟求助:水晶报表以word的格式到出到ie中时,xp,2000没问题的,但是在有些win98上有问题 一个模糊查找的存储过程,请问错在哪,进者有分 一个很菜的问题?关于处理字符串的问题!(急!在线等...) [帮忙看看][在线等待] 简单的SQL语句的问题! 关于Codebehind的问题,请赐教!
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();-------------------------------------------------------
//从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
FileStream fs=File.OpenRead(txtfile.Text);
byte[] content=new byte[fs.Length];
fs.Read(content, 0,content.Length);
fs.Close();
conn.Open();
string sql ="insert into Photos(name,Photo) values(@name, @Photos)";
SqlCommand comm=new SqlCommand(sql,conn);
comm.Parameters.Add("@Photos", SqlDbType.Image).Value=content;
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value=txtname.Text;
if(comm.ExecuteNonQuery()==1)
{
MessageBox.Show("Successfully insert image into database!");
}
else
{
MessageBox.Show("Failed to insert image into database");
}
conn.Close();读取图片:
private void button1_Click(object sender, System.EventArgs e)
{
ShowImage(txtname.Text);
} private void ShowImage(string s)
{
string str = "SELECT photo FROM Photos WHERE name='" + s +"'";
SqlCommand cmd = new SqlCommand(str, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream));
stream.Close();
}
conn.Close();
} private void DrawToScale(Image bmp)
{
pictureBox1.Image = new Bitmap(bmp);
}
这个是WinForm的实现方法,在WEBFORM下应该也差不多了,