将问题字节转换为字节

时间:2013-06-06 10:58:41

标签: asp.net sql-server c#-4.0

我从来没有和Bytes一起工作之前我遇到了错误,可能代码请看看

    SqlDataReader reader = cmd.ExecuteReader();
    reader.Read();
    SqlDataReader dr = ExecuteReader(Globals.con, CommandType.Text,
    "select FileName,MediaData,Extension from media where Id=" + ID);
    string FileName="";
    Byte[] MediaData= null;
    string Extension = "";
    while (dr.Read())
    {
        FileName = dr["FileName"].ToString();
        MediaData = Convert.ToByte(dr["MediaData"].ToString()); error is here
        Extension = dr["Extension"].ToString();              
    }
    dr.Close();
    string filename = (String)FileName;
    byte[] fileToDownload = (byte[])MediaData;
    String fileExtension = (String)Extension;

    in gridview i use this code below it working i need manual date
     not like code below
    string filename = (String)reader.GetValue(1);
    byte[] fileToDownload = (byte[])reader.GetValue(2);
    String fileExtension = (String)reader.GetValue(3);

请帮帮我吧

1 个答案:

答案 0 :(得分:1)

Convert.ToByte返回单个字节,而不是数组。

您还在使用ToString,它可以将二进制数据完全转换为您无法使用的表示形式:

MediaData = Convert.ToByte(dr["MediaData"].ToString())

应该是:

MediaData = (byte[])dr.Items["MediaData"];