将字节数组插入SqlServer 2008
问题描述:
在我的aspx页面中,我有一个使用此上传器的照片上传器我上传了图片。我必须在varbinary列中将此映像存储到DB(SqlServer 2008)中。我从该图像中读取所有字节并存储在字节数组中。如果我尝试将此字节数组插入到DB表中,我将收到错误。如何使用SQL查询插入...................将此字节数组插入数据库中
谢谢,
Velkumar
Hi,
In my aspx page, I have a photo uploader using this uploader I upload images. I have to store this image into DB(SqlServer 2008) in a varbinary column. I read all bytes from that image and store in a byte array. If i try to insert this byte array in to DB table i will get an error. How can I insert this byte array in to DB using SQL query " Insert into ..................."
Thanks,
Velkumar
答
几个有用的链接:
使用Microsoft .NET从SQL Server中检索和检索图像 [ ^ ]
http://forums.asp.net/t/1096999.aspx [ ^ ]
http://www.dotnetspider.com/forum/32610-Save-Image-into-database.aspx 一> [ ^ ]
http ://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx [ ^ ]
Few useful links:
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]
http://forums.asp.net/t/1096999.aspx[^]
http://www.dotnetspider.com/forum/32610-Save-Image-into-database.aspx[^]
http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx[^]
使用参数化查询(无论如何都应该避免SQL注入攻击) 。此代码将缩略图图像作为字节数组插入:
Use a parametrized query (you should be anyway to avoid SQL Injection attacks). This code inserts the Thumbnail image as a byte array:
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
{
con.Open();
byte[] bytes = Thumbnail.ToByteArray()
using (SqlCommand cmd = new SqlCommand("INSERT INTO Images (Id, Location, Thumbnail) VALUES (@ID, @DS, @TN)", con))
{
cmd.Parameters.AddWithValue("@ID", Id);
cmd.Parameters.AddWithValue("@DS", Location);
cmd.Parameters.AddWithValue("@TN", bytes);
cmd.ExecuteNonQuery();
}
}</pre>