当前位置: 代码迷 >> ASP.NET >> vs2003中,(byte[])datareader["image"]报错,无法转换,该如何解决
  详细解决方案

vs2003中,(byte[])datareader["image"]报错,无法转换,该如何解决

热度:5610   发布时间:2013-02-25 00:00:00.0
vs2003中,(byte[])datareader["image"]报错,无法转换
如题,vs2003中,(byte[])datareader["image"]报错,无法转换;该段代码在2005中正常。数据库为sql server 2005,字段类型image。
代码:
C# code
            string sqlText = "select pic,msg from WapPushMsg where id = " + id;            Odbc o = new Odbc();            byte[] photo = null;            using (OdbcDataReader dr = o.ExecuteReader(sqlText))            {                if (dr.Read())                {                    msgView.Text = dr[1].ToString();                    if (!dr.IsDBNull(0))                    {                        photo = (Byte[])dr[0];                                            }                    else                    {                    }                }            }


------解决方案--------------------------------------------------------
C# code
private void LoadImage(string fileName)        {            using (SqlConnection conn = new SqlConnection(connectionString))            {                string sql = "select blobdata from Image where ImageFileName like @filename";                 SqlCommand cmd = new SqlCommand(sql, conn);                 cmd.Parameters.Add("@filename", SqlDbType.Text);                 cmd.Parameters["@filename"].Value = fileName;                 conn.Open();                object objImage = cmd.ExecuteScalar();                byte[] buffer = (byte[])objImage;                 BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.png", FileMode.Create));                 bw.Write(buffer);                 bw.Close();               MemoryStream ms = new MemoryStream(buffer);                 Image bgImage = Image.FromStream(ms);                 ms.Close();                this.BackgroundImage = bgImage;             }                     }
------解决方案--------------------------------------------------------
pic字段类型是二进制的吗
  相关解决方案