数据库如何插入byte[]

deyter 2010-10-09 05:34:49
TestTable: ID, UserName, UserPhoto
直接用:string sql=" Insert into TestTable(UserName,UserPhoto) values ('Hello', PhotoData) ";
其中:PhotoData是byte[]类型。

中断看下,PhotoData的值为Insert into TestTable(UserName,UserPhoto) values ('Hello', System.Byte[]);

这个问题如何解决
谢谢
...全文
806 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
deyter 2010-10-11
  • 打赏
  • 举报
回复
谢谢各位了啊
qiuqingpo 2010-10-10
  • 打赏
  • 举报
回复
不错.收藏
jcrensheng 2010-10-09
  • 打赏
  • 举报
回复
public void imgToDB(string sql)
{ //参数sql中要求保存的imge变量名称为@images
//调用方法如:imgToDB("update TestTable set UserPhoto=@images where ID='" + temp + "'");
FileStream fs = File.OpenRead(t_photo.Text);
byte[] imageb = new byte[fs.Length];
fs.Read(imageb, 0, imageb.Length);
fs.Close();
SqlCommand com3 = new SqlCommand (sql,con);
com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;
if (com3.Connection.State == ConnectionState.Closed)
com3.Connection.Open();
try
{
com3.ExecuteNonQuery();
}
catch
{ }
finally
{ com3.Connection.Close(); }
}
wuyq11 2010-10-09
  • 打赏
  • 举报
回复
FileStream fs = new FileStream(@"D:\a.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
fs.Close();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into Photo(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = bt;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
conn.Close();
}
-小蕾- 2010-10-09
  • 打赏
  • 举报
回复
把参数PhotoData类型转换一下。当作参数传进去。
mywatermelon 2010-10-09
  • 打赏
  • 举报
回复
SQL的话……数据库的数据类型选择varbinary(MAX)……希望能够帮到楼主……
让爱延续 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 symbol_bc 的回复:]
C# code

SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
MemoryStream ms ……
[/Quote]
[Quote=引用 4 楼 flyerwing 的回复:]
参数化执行语句就好了!
[/Quote]
flyerwing 2010-10-09
  • 打赏
  • 举报
回复
参数化执行语句就好了!
symbol_bc 2010-10-09
  • 打赏
  • 举报
回复

SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,ImageFormat.Jpeg);
Byte[] bytBLOBData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0,null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

xiaotiange 2010-10-09
  • 打赏
  • 举报
回复
把photodata转换成 字符串

111,096

社区成员

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

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

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