如何使用asp.net在SQL 2005中保存图像 [英] How to save an image in SQL 2005 using asp.net
本文介绍了如何使用asp.net在SQL 2005中保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨亲爱的
我想使用asp.net网页在SQL2005中保存图片.
所以请帮帮我.
[edit]已删除呼喊"-OriginalGriff [/edit]
Hi dear
I want to save an image in SQL2005 using asp.net webpage.
So plz help me.
[edit]SHOUTING removed - OriginalGriff[/edit]
推荐答案
您需要创建一个带有图像列的表. coulmn数据类型应该是图像,然后在asp.net中使用以下代码
You need to create a table with column for image. The coulmn data type should be image then in your asp.net wtite following code
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
using (BinaryReader reader = new BinaryReader (FileUpload1.PostedFile.InputStream))
{
byte[] image = reader.ReadBytes (FileUpload1.PostedFile.ContentLength);
SaveImage(image);
}
}
}
private int SaveImage(byte[] image)
{
int rowsAffected;
using (SqlConnection connection = new SqlConnection("..."))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO (Photo) VALUES (@Photo)"; command.Parameters.AddWithValue("@Photo", image);
connection.Open();
rowsAffected = command.ExecuteNonQuery();
}
}
return rowsAffected;
}
尝试一下.
http://csharpdotnetfreak.blogspot.com/2009/07/fileupload-control- save-images-database.html [ ^ ].
没有什么比 ^ ].
Try this.
http://csharpdotnetfreak.blogspot.com/2009/07/fileupload-control-save-images-database.html[^].
Nothing is better than this[^].
感谢兄弟的帮助
这篇关于如何使用asp.net在SQL 2005中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文