2013-05-20 69 views
0

所以我设法建立了一个简单的测试页面来上传和存储图片到我的数据库,但是我不确定存储是否成功。图片上传到数据库

无论何时我将图像存储到我的表格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(); 
     } 

    } 
} 
+0

http://stackoverflow.com/search?q = user%3A31444 + httphandler + image – IrishChieftain

+1

我认为将图像存储在sperate文件夹中并将此图像的路径存储到数据库会更好。 – 2power10

+0

@imJustice尽管有它的好处,但当你有多个数据库实例,尝试备份或移动数据库时,它不能很好地扩展。您可以尝试使用[filestream](http://technet.microsoft.com/zh-cn/library/bb933993(v = sql.105).aspx)作为替代方法。 – gunr2171

回答

0

首先,在C#中Image型Db的映射为Byte[],所以你应该将图像转换为Byte[]插入到你的数据库之前。对于从数据库中检索你的形象,你可以使用代码:

MemoryStream stream = new MemoryStream(Byte[]);// you can read the image by dataadapter or datareader 
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); 

这里是一个很好的链接:http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html,希望它可以帮助你。

+0

我相信在存储之前我已经将图像转换为字节。 – Damienn

0

这总是帮助我。 'fileupload'是文件上传控制名称。

protected void Upload_btn(object sender, EventArgs e) 
{ 
    if (fileupload.HasFile) 
    { 
    if (fileupload.PostedFile.FileName == "") 
     {} 
    else 
     { 
     try 
     { 
      int filesize = fileupload.PostedFile.ContentLength; 
      if (filesize > 819200) 
      {Label1.Text = "File Size Should be Less than 800Kb"; 
      } 
      else 
      {string serverFileName = Path.GetFileName(fileupload.PostedFile.FileName); 
      byte[] documentBinary = new byte[filesize]; 
      fileupload.PostedFile.InputStream.Read(documentBinary, 0, filesize); 

      string mainquery = ""; 
      mainquery = "insert into table(DocName,DocSize,Data,ContentType)  values(@DocName,@DocSize,@Data,@ContentType)"; 
      con.Open(); 
      SqlCommand files_insert_cmd = new SqlCommand(mainquery,con); 
      files_insert_cmd.Parameters.AddWithValue("@DocName", serverFileName); 
      files_insert_cmd.Parameters.AddWithValue("@DocSize",fileupload.PostedFile.ContentLength); 
      files_insert_cmd.Parameters.AddWithValue("@Data", documentBinary); 
     files_insert_cmd.Parameters.AddWithValue("@ContentType",fileupload.PostedFile.ContentType); 
      files_insert_cmd.ExecuteNonQuery(); 

      con.Close(); 
     } 
    } 
    catch (Exception e1) 
     { 
     Label1.Text = e1.ToString(); 
     Label1.Visible = true; 
     } 

    } 
} 
} 
0

对不起,因为我要问一个愚蠢的问题。你如何检查表中的数据?你是否正确点击表格并选择编辑顶端200行 如果你这样做,它会一直显示为<二进制数据>在哪里运行SQL查询,你可以在varbinary(max)列或Image列中看到实际字符