Datareader无法读取图像值

时间:2014-04-21 21:04:55

标签: c# asp.net sql

我在页面加载时使用datareader来读取和存储变量中的数据库值,我的表包括nvarchar和image类型列。在页面加载时,数据库中的5个图像值不会被读取器读取,但其他图像值被完全读取。

Byte[] img1 = null;
Byte[] img2 = null;
Byte[] img3 = null;
Byte[] img4 = null;
Byte[] img5 = null;
SqlConnection con = new SqlConnection("Data Source=RAJ-PC\\SQLEXPRESS;Initial Catalog=Finder;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        loadad();
    }
}

protected void loadad()
{
    SqlCommand cmd = new SqlCommand("sps_addetails", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ad_id", ad_id);
    cmd.Parameters.AddWithValue("@useremail", ses);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        rd_iam.SelectedValue = reader["iam"].ToString();
        dd_category.SelectedValue = reader["category"].ToString();
        c = Convert.ToInt16(reader["category"].ToString());
        dd_subcategory.SelectedValue = reader["subcategory"].ToString();
        txt_title.Text = reader["title"].ToString();
        txt_description.Text = reader["description"].ToString();
        txt_pername.Text = reader["contactname"].ToString();
        txt_mobile1.Text = reader["mobile1"].ToString();
        txt_mobile2.Text = reader["mobile2"].ToString();
        txt_landline1.Text = reader["landline1"].ToString();
        txt_landline2.Text = reader["landline2"].ToString();
        txt_email1.Text = reader["email1"].ToString();
        txt_email2.Text = reader["email2"].ToString();
        txt_website.Text = reader["website"].ToString();
        dd_country.Text = reader["country"].ToString();
        d = Convert.ToInt16(reader["country"].ToString());
        dd_state.Text = reader["state"].ToString();
        txt_pincode.Text = reader["pincode"].ToString();
        txt_address.Text = reader["address"].ToString();
        txt_lat.Text = reader["latitude"].ToString();
        txt_lon.Text = reader["longitude"].ToString();

        img1 = (byte[])reader["image1"];
        img2 = (byte[])reader["image2"];
        img3 = (byte[])reader["image3"];
        img4 = (byte[])reader["image4"];
        img5 = (byte[])reader["image5"];

    }
    con.Close();

}

存储过程sps_addetails是

ALTER PROCEDURE [dbo].[sps_addetails] 
@ad_id int,
@useremail nvarchar(100)
AS
BEGIN
select * from dbo.tbl_adregister where useremail=@useremail and ad_id=@ad_id
END

ASPX

<asp:FileUpload ID="FileUpload1" runat="server" />

更新按钮功能是(aspx.cs)

Byte[] imgbytes1 = null;
        if (FileUpload1.HasFile)
        {
            HttpPostedFile file1 = FileUpload1.PostedFile;
            imgbytes1 = new Byte[file1.ContentLength];
            file1.InputStream.Read(imgbytes1, 0, file1.ContentLength);
        }
        else
        {
            imgbytes1 = img1;
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("sps_uploadphoto", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@imagedata1", imgbytes1);
        cmd.ExecuteNonQuery();
        con.Close();

2 个答案:

答案 0 :(得分:0)

首先尝试确定图像大小,然后实例化变量,然后使用GetBytes()方法:

int index = reader.GetOrdinal("image1");
Int64 size = 0;

try { size = reader.GetBytes(index , 0, null, 0, int.MaxValue); }
catch { size = reader.GetBytes(index, 0, img1, 0, 1); }

img1 = new Byte[size];

reader.GetBytes(index, 0, img1, 0, img1.Length);

... rinse and repeat for images 2 through 5 ...

如果能为您提供,请告诉我们。

答案 1 :(得分:0)

阅读图像价值与其他人不同......

<img runat="server" id="image1" alt="" src="" height="100" width="100" />

protected void LoadImage1()
{
    SqlCommand cmd = new SqlCommand("sps_getimage", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@flag", 1);
    cmd.Parameters.AddWithValue("@ad_id", ad_id);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
    if (reader.HasRows)
    {
        reader.Read();
        MemoryStream memory = new MemoryStream();
        long startIndex = 0;
        const int ChunkSize = 256;
        while (true)
        {
            byte[] buffer = new byte[ChunkSize];
            long retrievedBytes = reader.GetBytes(0, startIndex, buffer, 0, ChunkSize);
            memory.Write(buffer, 0, (int)retrievedBytes);
            startIndex += retrievedBytes;
            if (retrievedBytes != ChunkSize)
                break;
        }

        byte[] data = memory.ToArray();
        img1 = data;
        memory.Dispose();
        image1.Src = "data:image/png;base64," + Convert.ToBase64String(data);
    }
    con.Close();
}