使用asp.net从sql数据插入/更新/删除图像

时间:2017-02-12 10:32:29

标签: c# asp.net

我想插入/删除,使用asp.net在sql数据库中更新图像。我的插入代码是:

    Stream strm = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(strm);
        byte[] img = br.ReadBytes(Convert.ToInt32(strm.Length)); 
        imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(img, 0, img.Length);
        imgDetail.Visible = true;
        try
        {
            cmd = new SqlCommand("insert into Persons (id,Name,Surname,Address,Phone,Picture,Department) values (@id,@Name,@Surname,@Address,@Phone,@img,@Department)",con);
            cmd.Parameters.AddWithValue("id", txtId.Text);
            cmd.Parameters.AddWithValue("Name", txtName.Text);
            cmd.Parameters.AddWithValue("Surname", txtSurname.Text);
            cmd.Parameters.AddWithValue("Address", txtAddress.Text);
            cmd.Parameters.AddWithValue("Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("img", img);
            cmd.Parameters.AddWithValue("Department", Department.Text);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

我认为添加的图像出了问题,也许我做错了。现在当我点击gridview中的某些记录时,记录的所有数据都会显示在文本框中,但是我无法在图像框中显示图像。我怎样才能做到这一点? 这是在文本框中替换数据的一部分:

     foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowIndex == GridView1.SelectedIndex)
            {
                GridViewRow row1 = GridView1.SelectedRow;
                txtId.Text = row.Cells[0].Text;
                txtName.Text = row.Cells[1].Text;
                txtSurname.Text = row.Cells[2].Text;
                txtAddress.Text = row.Cells[3].Text;
                txtPhone.Text = row.Cells[4].Text;
                Department.Text = row.Cells[5].Text;
                Image.??????

1 个答案:

答案 0 :(得分:0)

&#34;图像&#34; control只是标准<img> HTML标记的包装器。显然它没有任何二进制属性,你可以设置为字节数组。

但你选择了&#34;数据&#34; URI。

以下是您的想法:

Image.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])imagedata);
相关问题