【问题标题】:Retrieve LONGBLOB from MySQL in C#在 C# 中从 MySQL 中检索 LONGBLOB
【发布时间】:2012-01-05 16:11:47
【问题描述】:

我想从我的 MySQL 数据库中检索一个 LONGBLOB,但我不知道如何。我搜索了 interwebz 并没有发现任何真正有用的(可以理解的)。当我检索 LONGBLOB 时,我想将其另存为图像。

这是我已经尝试过的:

        int bufferSize = 100;
        byte[] bin = new byte[bufferSize];
        long retval = 0;
        long startIndex = 0;

        MemoryStream ms = null;
        Image image = null;

        MySqlCommand command = new MySqlCommand("select * from image where uid = @uid", Connection.Connect());
        command.Parameters.AddWithValue("@uid", "2");
        MySqlDataReader reader = command.ExecuteReader();

        if (reader.Read())
        {
            retval = reader.GetBytes(reader.GetOrdinal("logo"), startIndex, bin, 0, bufferSize);
        }


        ms = new MemoryStream(bin);
        image = Image.FromStream(ms);

提前致谢。

【问题讨论】:

    标签: c# mysql blob


    【解决方案1】:

    我不确定,但我想你可以试试这个:http://www.techrepublic.com/article/utilize-adonet-and-c-to-work-with-blob-data/5766889

    斯蒂芬

    【讨论】:

    • 出现错误:image = Image.FromStream(ms); (参数无效)。
    【解决方案2】:

    我实际上是作为我正在从事的项目的一部分这样做的......

    公共位图加载图像(int imgID) { MySqlDataReader 我的数据; MySqlCommand cmd = new MySqlCommand(); 字符串 SQL; 字节[] 原始数据; 内存流毫秒; UInt32 文件大小; 位图输出图像; SQL = "从图像中选择图像名称、图像大小、图像 WHERE ImageID ="; SQL += imgID.ToString(); 尝试 { cmd.Connection = 连接; cmd.CommandText = SQL; myData = cmd.ExecuteReader(); 如果(!myData.HasRows) throw new Exception("没有要保存的 blob"); myData.Read(); FileSize = myData.GetUInt32(myData.GetOrdinal("ImageSize")); rawData = 新字节[文件大小]; myData.GetBytes(myData.GetOrdinal("Image"), 0, rawData, 0, (Int32)FileSize); 毫秒 = 新的内存流(原始数据); outImage = 新位图(毫秒); ms.Close(); ms.Dispose(); 我的数据。关闭(); myData.Dispose(); cmd.Dispose(); 返回输出图像; } 捕捉(MySqlException ex) { MessageBox.Show(ex.Message); 返回空值; } }

    希望这会有所帮助。另外请原谅任何不好的编码习惯,这是不久前写的。

    谢谢 汤姆

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 2018-05-26
      • 2015-10-05
      相关资源
      最近更新 更多