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();
}
}
}
}
我在做一个图片上传的功能,可是有问题,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();
}
}
}
}
解决方案 »
- 高手们,怎么使用SQL语句操作处于两个ACCESS数据库中的表
- Process.Start()的诡异问题。
- 新浪博客自动访问留脚印
- 想用GDI实现类似播放器显示区域中的,音量的音柱效果。谁有例子给发个?
- 如何实现更改文件或文件夹的所有者
- c#写的windows form程序怎么样可以实现像WINDOWS服务一样,无需登陆WINDOWS用户即可自动启动?
- 【紧急求助】WinForm控件库在IE中使用的问题,急!!!
- 一個B/S的ERP需要做優化與系計,各位一起來討論一下
- 一个关于C#中通过Parameter方式访问数据库的问题
- svn服务器端配置
- TreeView重绘问题,急啊
- 把程序拷贝到别人的机器上连接数据库出现问题(求救!)
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();
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();