C# 图像上传到数据库

C# 图像上传到数据库,c#,asp.net,sql,C#,Asp.net,Sql,所以我设法设置了一个简单的测试页面,将图像上传并存储到我的数据库中,但我不确定存储是否成功 每当我使用数据类型image将图像存储到表中image列下时,这就是新行中的内容。这是图像吗 我希望它在“0”和“1”中显示图像,以便比较存储的不同项目。但是,存储“”是否意味着成功存储了我的图像 我的网站的逻辑是用c#编码的 同时,我也一直在试图找到一些源代码,并举例说明如何检索图像以供显示 这是我当前的insert语句 SqlCommand com = new SqlCommand("insert i

所以我设法设置了一个简单的测试页面,将图像上传并存储到我的数据库中,但我不确定存储是否成功

每当我使用数据类型
image
将图像存储到表中
image
列下时,这就是新行中的内容
。这是图像吗

我希望它在“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();**  
因此,对于图像,它会是什么样子

编辑:单击事件上的完整按钮以处理图像条纹

    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();
        }

    }
}

首先,Db中的
图像
类型映射到C#中的
字节[]
,因此在插入数据库之前,您应该将图像转换为
字节[]
。要从数据库检索图像,可以使用以下代码:

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

这里是一个很好的链接:希望它能帮助您。

这里是一个完整的教程


这对我总是有帮助的。”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;
      }

   }
 }
}

事先对不起,因为我要问一个愚蠢的问题。如何检查表中的数据?您是否右键单击表格并选择“编辑前200行”
如果您这样做,它将始终显示为,在这里,如果您运行sql查询,您可以在varbinary(max)列或Image列中看到实际字符

,我认为最好将图像存储在sperate文件夹中,并将此图像的路径存储到数据库中。@imJustice,虽然这确实有其好处,当您有多个数据库实例、尝试对其进行备份或移动数据库时,它无法很好地扩展。您可以尝试使用作为替代方法。@imJustice,他需要源代码管理,因为在prod和dev之间保持图像同步是一件非常痛苦的事情。@gunr217您所说的多个数据库实例到底是什么意思?是因为很多用户同时连接到我的数据库吗?我相信我在存储之前已经将图像转换为字节了。