【问题标题】:C# ODBC Get tinyblob and blob as byte arrayC# ODBC 获取 tinyblob 和 blob 作为字节数组
【发布时间】:2014-06-26 01:30:44
【问题描述】:

我有以下问题。 我有一个有 26 列的表。一些列是 tinyblob 或 blob 列。存储在这些 blob 列中的数据格式为:

0000 0010 0005 0400 0365 0000 0020 0041 0251 0023

我使用以下代码来获取我的选择行:

            try

             {

                 string[] CurrentCons;
                 mysql.command = new OdbcCommand("SELECT * FROM current.cons_list WHERE name = ?", mysql.connection);
                 mysql.command.Parameters.Add("name", OdbcType.VarChar).Value = nick;
                 mysql.myReader = mysql.command.ExecuteReader();
                 CurrentCons = new string[mysql.myReader.FieldCount];
                 mysql.myReader.Read();

                 for (int i = 0; i < CurrentCons.Length; i++)
                 {
                     CurrentCons[i] = mysql.myReader[i].ToString();
                 }
                 return CurrentCons;
             }
             catch(Exception ex)
             {
                 string[] CurrentCons;
                 LOG.WriteLog("(" + DateTime.Now + ")\t" + ex.Message + "\t" + ex.Source + "\t" + ex.InnerException + "\n");
                 CurrentCons = new string[0];
                 return CurrentCons;
             }

我想要做的是将 tinyblob 作为字节数组并将其称为一个,例如

byte[] MyBLOB = functions.GetData("John213");
MessageBox.Show(string.format("{0}", MyBLOB[0])); //0
MessageBox.Show(string.format("{0}", MyBLOB[2])); //5
MessageBox.Show(string.format("{0}", MyBLOB[4])); //869

这可能吗?提前谢谢你。

【问题讨论】:

    标签: c# arrays odbc byte blob


    【解决方案1】:

    将 blob 读入字节数组的方法是通过GetBytes() 方法

    int length; //the width of the blob
    byte[] buffer = new byte[length];
    reader.GetBytes(reader.GetOrdinal("John123"), 0, buffer, 0, length);
    

    MSDN Documentation for the method

    仅供参考,GetBytes() 可用于 C# 中的大多数 DataReader 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2016-07-16
      • 1970-01-01
      • 2016-08-20
      相关资源
      最近更新 更多