关于二进制存取图片到数据库的问题

chuzhongbiyewenhua 2010-06-25 04:31:13
FileStream fs = File.OpenRead(@"D:\sasda.jpg");
byte[] source = new byte[(int)fs.Length];
fs.Read(source, 0, (int)fs.Length);
我再直接把source 插入数据库的image类型的列里面,不行啊
...全文
144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianuMan 2010-06-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chuzhongbiyewenhua 的回复:]
你这个发的是存图片路径的好不
[/Quote]

51. string commandText = "Insert into ImageTable(ImagePath, Image)Values(@ImagePath, @Image)";
52. SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection);
53. sqlCommand.Parameters.Add("@ImagePath", SqlDbType.Text);
54. sqlCommand.Parameters.Add("@Image", SqlDbType.Binary);
55. sqlCommand.Parameters["@ImagePath"].Value = file;
56. sqlCommand.Parameters["@Image"].Value = byteImage;

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jianuMan/archive/2010/06/13/5670061.aspx

我这个是路径和图片都存了的, 看红色的地方
loveSoftandhxy 2010-06-26
  • 打赏
  • 举报
回复
数据库 字段类型
chuzhongbiyewenhua 2010-06-26
  • 打赏
  • 举报
回复
你这个发的是存图片路径的好不
wangzhe117 2010-06-26
  • 打赏
  • 举报
回复
//从数据库取图片

string sql_zp = "select YZImg from Set_YZImg where YZImgBH='" + this.txtZPBH.Text.Trim() + "'";
DataTable table_zp = dbhelper.GetDataTableSQL(sql_zp);
if (table_zp.Rows.Count > 0)
{
byte[] byte_img = (byte[])table_zp.Rows[0]["YZImg"];
MemoryStream ms = new MemoryStream(byte_img);
Image img = Image.FromStream(ms);
this.pictureImg.Image = img;
}
改一下查询语句就行了
jianuMan 2010-06-25
  • 打赏
  • 举报
回复
chuzhongbiyewenhua 2010-06-25
  • 打赏
  • 举报
回复
求救啊
xy325432 2010-06-25
  • 打赏
  • 举报
回复
学习,帮顶。。
卧_槽 2010-06-25
  • 打赏
  • 举报
回复
最近怎么老看到类似的问题,是不是哪个白痴老师布置的作业咯?
chuzhongbiyewenhua 2010-06-25
  • 打赏
  • 举报
回复
FileStream fs = new FileStream(@"D:\sasda.jpg", FileMode.Open, FileAccess.Read);
byte[] source = new byte[(int)fs.Length];
fs.Read(source, 0, (int)fs.Length);
if (T_MapManager.InsertMap(source))
{
MessageBox.Show("sds");
}

Image image = Image.FromStream(fs);
picImage.Image = image;

fs.Close();


public static bool InsertMap(byte[] map)
{
string sql = string.Format("INSERT INTO T_Map (id, imagename, imageflag) VALUES (1,'{0}',1)",map);
return SQLHelper.DoUpdate(sql) > 0;
}
map是我读好的2进制,但是 存入数据库就只有0x53797374656D2E427974655B5D这个长的字符,为什么
zhengzhiqiangcn 2010-06-25
  • 打赏
  • 举报
回复
int intFileLength = FileUpload1.PostedFile.ContentLength;
//获取完全名称
string Filename = FileUpload1.PostedFile.FileName;
//获取文件名称
string Filename1 = FileUpload1.FileName;
Stream objstream;
byte[] docbuffer = new byte[intFileLength];
objstream = FileUpload1.PostedFile.InputStream;
objstream.Read(docbuffer, 0, intFileLength);//将文件读入buffer中
objstream.Close();
string myconnectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
SqlConnection myy = new SqlConnection(myconnectionstring);
SqlCommand xx = new SqlCommand("cc", myy);//存储过程实现写入
xx.CommandType = CommandType.StoredProcedure;
xx.Parameters.Add("@title", SqlDbType.NVarChar, 50);
xx.Parameters.Add("@doc", SqlDbType.Image);
xx.Parameters[0].Value = Filename1;
xx.Parameters[1].Value = docbuffer;

myy.Open();
int j = xx.ExecuteNonQuery();
myy.Close();

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧