使用varbinary使用FileUpload将映像检索到数据库

时间:2016-04-14 00:28:25

标签: c# sql asp.net varbinary

好的,我正在使用

将其添加到数据库中
  HttpPostedFile postedFile = eventImage.PostedFile;
        string fileExtension = Path.GetExtension(postedFile.FileName);

        if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".png")
        {
            Stream stream = postedFile.InputStream;
            BinaryReader reader = new BinaryReader(stream);
            byte[] imgByte = reader.ReadBytes((int)stream.Length);
            con = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-StudentMoneySaver-20160203040444.mdf;Initial Catalog=aspnet-StudentMoneySaver-20160203040444;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into Events (AspNetUsersId,EvtName,EvtType,EvtDescription,EvtDate,EvtVote, EvtImage) values (@AspNetUsersId, @EvtName, @EvtType, @EvtDescription, @EvtDate, @EvtVote, @EvtImage)", con);

            cmd.Parameters.AddWithValue("@AspNetUsersId", userId);
            cmd.Parameters.AddWithValue("@EvtName", eventName.Text);
            cmd.Parameters.AddWithValue("@EvtType", eventType.Text);
            cmd.Parameters.AddWithValue("@EvtDescription", eventDescription.Text);
            cmd.Parameters.AddWithValue("@EvtDate", datetimepicker.Value);
            cmd.Parameters.AddWithValue("@EvtVote", 0);
            cmd.Parameters.Add("@EvtImage", SqlDbType.VarBinary).Value = imgByte;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

并尝试使用

调用图像
  byte[] imgByte = null;
        con = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-StudentMoneySaver-20160203040444.mdf;Initial Catalog=aspnet-StudentMoneySaver-20160203040444;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("SELECT * FROM Events", con);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            string str = Convert.ToBase64String(imgByte);
            imageTest.Src = "data:Image/png;base64," + str;
        }

前端代码为

<img runat="server" id="imageTest" src="imageIDtagName" />

我收到错误消息&#34;值不能为空。 参数名称:inArray

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:inArray&#34;我想我已经结束了!

Events Table

1 个答案:

答案 0 :(得分:4)

如果要从SQL加载Image,可以这样做:

byte[] imgByte = null;

SqlCommand cmd = new SqlCommand("SELECT Image FROM tableName",yourConnectionStringHere);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.fill(ds);
foreach(DataRow dr in ds.Tables[0].Rows)
{
  imgByte = (byte[])(dr["ImageColumnNameHere"].ToString());
  string str = Convert.ToBase64String(imgByte);
  imageIDtagName.Src = "data:Image/png;base64," + str;
}

使用文件上传保存图像的方式如下:

HttpPostedFile postedFile = imgFile.PostedFile;
string fileExtension = Path.GetExtension(postedFile.FileName);

 if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".png")
 {
            Stream stream = postedFile.InputStream;
            BinaryReader reader = new BinaryReader(stream);
            byte[] imgByte = reader.ReadBytes((int)stream.Length);
     SqlCommand cmd = new Sqlcommand("INSERT INTO tableName(Image) VALUES(@img)",yourConnectionStringHere);
cmd.Parameters.Add("@img",SqlDbType.VarBinary).Value = imgByte;
     cmd.ExecuteNonQuery();
 }

P.S。使用类似

的语句来覆盖sqlconnection
using(SqlConnection con = new SqlConnection(""))
{
   //your code here
}

让您免于执行con.Open()con.Close()

编辑: 要访问html代码的 src 属性,只需在html代码中添加runat="server"即可。