byte[] 怎么用insert into 语句插入?

mrhu7002 2007-11-15 09:07:41
byte[] 怎么用insert into 语句插入?
我在做一个图片上传的功能,可是有问题,byte[]类型的图片内容数据在insert into 的时候变成了System.Byte[] 字符串
这是我的源代码
    
protected int imageSize;
protected byte[] imageBody;
protected string SqlCom = "";
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{
//创建数据库链接对象
con = new SqlConnection(ConfigurationManager.ConnectionStrings["databasecon"].ToString());
}

//图片上传
protected void BT_UpFile_Click(object sender, EventArgs e)
{
string imagePath;
string imageType;
string imageName;
if (UpFileControl.PostedFile.FileName != "")
{
//取得选择的图片路径
imagePath = UpFileControl.PostedFile.FileName;
//取得选择的图片扩展名
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
//取得选择的图片名
imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);

if ("jpg" != imageType && "gif" != imageType)
{
Response.Write("对不起!请您选择JPG或者GIF格式的图片!");
return;
}
else
{
//建立访问客户端上传文件的对象
HttpPostedFile SendImage = UpFileControl.PostedFile;
//取得图片的大小
imageSize = SendImage.ContentLength;
imageBody = new Byte[imageSize];
//建立数据流对象
Stream StreamObject = SendImage.InputStream;
//把图像数据放到imageBody中,其中0代表数据指针位置,ImageSize代表要读取的流的长度
StreamObject.Read(imageBody, 0, imageSize);
SqlCom = "insert into Images(ImageName,Images,ImageSize,imgtype)";
SqlCom += " VALUES('" + imageName + "','" + imageBody + "','" + imageSize.ToString() + "','" + imageType + "')";
con.Open();
cmd = new SqlCommand(SqlCom, con);
try
{
cmd.ExecuteNonQuery();
Response.Write("成功!");
}
catch
{
Response.Write("失败!");
}
finally
{
con.Close();
}
}
}
}


...全文
713 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mrhu7002 2007-11-15
  • 打赏
  • 举报
回复
大家让我恍然大悟啊,天天用的东西居然一时忘记了,真是没面子``````````谢谢黑马王子和你的武器已破碎
SDFDSAC 2007-11-15
  • 打赏
  • 举报
回复
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string imgtitle = TextBox1.Text;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand
         ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
         VALUES ( @imgtitle, @imgtype,@imgdata )", connection );

SqlParameter paramTitle = new SqlParameter
         ("@imgtitle", SqlDbType.VarChar,50 );

paramTitle.Value = imgtitle;
command.Parameters.Add( paramTitle);

SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );

SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );

connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
boblaw 2007-11-15
  • 打赏
  • 举报
回复
参考下面的方法

byte[] imagebody;
SqlCommand cmd = new SqlCommand("insert into tbname(col1,col2) values(@col1,@col2)");
cmd.Parameters.Add("@col1", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@col2",SqlDbType.Image).Value=imagebody;
cmd.ExecuteNonQuery();
mrhu7002 2007-11-15
  • 打赏
  • 举报
回复
参数化插入怎么在C#里做呢?能不能给个例子?
boblaw 2007-11-15
  • 打赏
  • 举报
回复
使用存储过程,或者使用参数方式插入

111,098

社区成员

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

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

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