1

在我在可能的代码中出现错误之前,我从不使用 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);

请帮帮我

4

1 回答 1

1

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

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

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

应该:

MediaData = (byte[])dr.Items["MediaData"];
于 2013-06-06T11:02:54.443 回答