所以我设法建立了一个简单的测试页面来上传和存储图片到我的数据库,但是我不确定存储是否成功。图片上传到数据库
无论何时我将图像存储到我的表格Image
的列012xx下,这就是新行<Binary data>
中的内容。这是<Binary data>
的图像?
我期待它显示'0'和'1'图像,以便我可以比较存储的不同项目。但存储是否意味着我的映像已成功存储?
我的网站的逻辑编码在C#中。
而且我一直在试图找到具有示例的来源,以了解如何检索用于显示的图像。
这是我目前的INSERT语句
SqlCommand com = new SqlCommand("insert into ImageTotable "
+ "(myphoto,name) values (@photo, @name)", con);
检索数据将这项工作?
SqlCommand com2 = new SqlCommand("Select * from ImageTotable WHERE userid ='1'", con);
所以,如果我使用DataReader来存储选择的项目,我能保存我的形象,使其显示,标签,图像按钮,等?
如何将图像存储到变量中?例如,如果我想保存的文本,我会用:
pw = dr["password"].ToString();**
因此,对于图像会是什么样的呢?
编辑:上单击事件全部按钮来处理图像strage
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=*;Initial Catalog=*;Integrated Security=True");
if (!FileUpload1.HasFile)
{
Label1.Visible = true;
Label1.Text = "Please Select Image File"; //checking if file uploader has no file selected
}
else
{
int length = FileUpload1.PostedFile.ContentLength;
byte[] pic = new byte[length];
FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
try
{
con.Open();
SqlCommand com = new SqlCommand("insert into ImageTotable "
+ "(myphoto,name) values (@photo, @name)", con);
com.Parameters.AddWithValue("@photo", pic);
com.Parameters.AddWithValue("@name", TextBox1.Text);
com.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Image Uploaded Sucessfully"; //after Sucessfully uploaded image
}
finally
{
con.Close();
}
}
}
http://stackoverflow.com/search?q = user%3A31444 + httphandler + image – IrishChieftain
我认为将图像存储在sperate文件夹中并将此图像的路径存储到数据库会更好。 – 2power10
@imJustice尽管有它的好处,但当你有多个数据库实例,尝试备份或移动数据库时,它不能很好地扩展。您可以尝试使用[filestream](http://technet.microsoft.com/zh-cn/library/bb933993(v = sql.105).aspx)作为替代方法。 – gunr2171