CUBRID学习笔记 15 Lobs类型数据

  • BLOB: Binary large object
  • CLOB: Character large object 
  • 一个二进制 一个字符类型

 

  二进制的读取

CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    CUBRIDBlob bImage = (CUBRIDBlob)reader[0];
    byte[] bytes = new byte[(int)bImage.BlobLength];
    bytes = bImage.getBytes(1, (int)bImage.BlobLength);
    //...
}<br><br><br>更新  clob类型
string sql = "UPDATE t SET c = ?";
CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);
 
CUBRIDClob Clob = new CUBRIDClob(conn);
 
//Use the ConnectionString for testing
str = "server=localhost;database=demodb;port=33000;user=public;password="
 
Clob.setString(1, str);
CUBRIDParameter param = new CUBRIDParameter();
param.ParameterName = "?";
param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB;
param.Value = Clob;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
<br>下面来看个完整的例子
using CUBRID.Data.CUBRIDClient;
using System.Diagnostics;
using System;
using System.IO;
using System.Data.Common;
 
namespace LobExample
{
    class Program
    {
        private static void ExecuteSQL(string sql, CUBRIDConnection conn)
        {
            using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
 
        private static void CreateTestTableLOB(CUBRIDConnection conn)
        {
            ExecuteSQL("drop table if exists t", conn);
            ExecuteSQL("create table t(b BLOB, c CLOB)", conn);
        }
 
        private static void CleanupTestTableLOB(CUBRIDConnection conn)
        {
            ExecuteSQL("drop table if exists t", conn);
        }
 
        static void Main(string[] args)
        {
            CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder("localhost", "demodb", "public", "", "33000");
            using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))
            {
                conn.Open();
 
                CreateTestTableLOB(conn);
 
                string sql = "insert into t (c) values(?)";
                CUBRIDCommand cmd = new CUBRIDCommand(sql, conn);
 
                CUBRIDClob Clob = new CUBRIDClob(conn);
 
                String str = conn.ConnectionString;
 
                StreamReader r = new StreamReader("test.txt");
                string writestring = r.ReadToEnd();
                r.Close();
 
                Clob.setString(1, writestring);
 
                CUBRIDParameter param = new CUBRIDParameter();
                param.ParameterName = "?";
                param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_CLOB;
                param.Value = Clob;
                cmd.Parameters.Add(param);
                cmd.ExecuteNonQuery();
                cmd.Close();
 
                string sql2 = "SELECT c from t";
                using (CUBRIDCommand cmd2 = new CUBRIDCommand(sql2, conn))
                {
                    DbDataReader reader = cmd2.ExecuteReader();
 
                    while (reader.Read())
                    {
                        CUBRIDClob cImage = (CUBRIDClob)reader[0];
                        string str2 = cImage.getString(1, (int)cImage.ClobLength);
 
                        StreamWriter w = new StreamWriter("testout.txt");
                        w.Write(str2);
                        w.Close();
 
                        StreamReader r2 = new StreamReader("testout.txt");
                        string readstring = r2.ReadToEnd();
                        r2.Close();
 
                        Debug.Assert(writestring.Length == readstring.Length, "The inserted CLOB length is not valid!");
                        Debug.Assert(writestring.Equals(readstring), "The CLOB was not inserted correctly!");
                    }
                }
 
                CleanupTestTableLOB(conn);
 
                conn.Close();
            }
        }
    }
}
<br><br><br><br><br>
posted @   过错  阅读(290)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示