.NET OracleLob 读写操作

本文内容

  • OracleLob 操作 CLOB 字段
  • OracleLob 操作 BLOB 字段

 

OracleLob 操作 CLOB 字段

向 Oracle Clob 字段写入数据
string connStr = "Data Source=am;uid=scott;pwd=1;unicode=true";
string id = string.Empty;
OracleConnection conn = new OracleConnection(connStr);
OracleCommand cmd = conn.CreateCommand();
 
// 字符串
id = Guid.NewGuid().ToString();
StringBuilder data = new StringBuilder();
for (int i = 1; i <= 100; i++) data.Append("a");
 
try
{
    conn.Open();
    //cmd.CommandText = "DROP TABLE mylobtable";
    //cmd.CommandType = CommandType.Text;
    //cmd.ExecuteNonQuery();
    //Response.Write("删除表'mylobtable'成功.
");
 
    cmd.CommandText = "CREATE TABLE mylobtable (a varchar2(36), b clob,c blob)";
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
    Response.Write("创建表 'mylobtable' 成功.
"
);
 
    // 一般方式
    cmd.CommandText = "INSERT INTO mylobtable(a,b) VALUES('" + id + "','" + data.ToString() + "')";
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
    Response.Write("添加成功.
"
);
 
    // 使用 OracleParameter OracleType.Clob
    cmd.CommandText = "INSERT INTO mylobtable(a,b) VALUES('" + id + "'," + ":data)";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Clear();
    OracleParameter paraClob = new OracleParameter("data", OracleType.Clob);
    paraClob.Value = data.ToString();
    cmd.Parameters.Add(paraClob);
    cmd.ExecuteNonQuery();
    Response.Write("添加成功.
"
);
 
    // 使用 OracleParameter OracleType.NVarChar
    cmd.CommandText = "INSERT INTO mylobtable(a,b) VALUES('" + id + "'," + ":data)";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Clear();
    OracleParameter paraNVarChar = new OracleParameter("data", OracleType.NVarChar);
    paraNVarChar.Value = data.ToString();
    cmd.Parameters.Add(paraNVarChar);
    cmd.ExecuteNonQuery();
    Response.Write("添加成功.
"
);
}
catch (Exception ex)
{
    Response.Write("失败." + ex);
}
finally
{
    cmd.Dispose();
    conn.Close();
    conn.Dispose();
}

说明

1) 演示用三种方式向 Oracle 表 mylobtable 的 Clob 字段写入内容,这三种方式都有各自的特点,或是说限制。

2) 首先删除 mylobtable,在创建该表。

3) 直接构造 SQL 语句,向该表插入一条记录。这种方式,在 Oracle 9i 中,超过 2000 个字符就会报错。而在 Oracle 11g下,则是超过 4000 才会报错;

4) 之后用带参数的方式,向该表插入一条记录。因为 Clob 字段存储的字符,所以你可以用 OracleType.NVarChar 插入记录,也可以用 Oracle.Clob 插入。当用 Oracle.Clob 插入时,在 Oracle 9i 下,没什么问题,但如果写入4亿个字符,就超出我电脑的内存(2G);9千万个字符就执行得很慢。而在 Oracle 11g 下,执行 9 千万个字符似乎挺快的。

5) 当用 Oracle.NVarChar 时。在 Oracle 9i 下,超过 2000 个字符就会出错,而在 Oracle 11g 下,超过9千万都没有事。

从 Oracle Clob 字段读取数据
string connStr = "Data Source=am;uid=scott;pwd=1;unicode=true";
string sqlStr = "SELECT b FROM mylobtable";
 
OracleConnection conn = new OracleConnection(connStr);
OracleDataAdapter da = new OracleDataAdapter(sqlStr, conn);
OracleCommand cmd = new OracleCommand(sqlStr, conn);
OracleDataReader reader = null;
cmd.CommandText = sqlStr;
cmd.CommandType = CommandType.Text;
 
int actual = 0;
DataTable dt = new DataTable();
try
{
    conn.Open();
    da.Fill(dt);
    Response.Write("不使用 OracleLob:
"
);
    foreach (DataRow dr in dt.Rows)
    {
        Response.Write(dr["b"].ToString() + "
"
);
    }
    reader = cmd.ExecuteReader();
 
    Response.Write("使用 OracleLob:
"
);
    while (reader.Read())
    {
        OracleLob clob = reader.GetOracleLob(0);
        if (clob != OracleLob.Null)
        {
            Response.Write(clob.Value + " ");
            StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
            char[] cbuffer = new char[9];
            while ((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) > 0)
            {
                Response.Write(clob.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " +
                cbuffer.Length + ") => " + actual + " ");
            }
        }
    }
 
}
catch
{
    Response.Write("
读取CLOB字段失败.
"
);
}
finally
{
    conn.Close();
    conn.Dispose();
    da.Dispose();
    reader.Dispose();
    cmd.Dispose();
}

说明

(1) 演示用两种方法读取 CLOB 字段的内容,但如果仔细区分的话,则是三种;

(2) 第一种方法是不使用 OracleLob 类;第二种方法可以具体区分为两种,一是使用 OracleLob.Value,二是使用 StreamReader 类;

(3) 因为 OracleLob 类直接继承 .Net Stream 类,所以,所有操作 Stream 的存在的类都可是被使用。.Net StreamReader 把 raw bytes 转换成实际字符很容易。

 

OracleLob 操作 BLOB 字段

向 Oracle Blob 字段写入数据
string connStr = "Data Source=am;uid=scott;pwd=1;unicode=true";
string id = string.Empty;
OracleConnection conn = new OracleConnection(connStr);
OracleCommand cmd = conn.CreateCommand();
 
// 字符串
id = Guid.NewGuid().ToString();
StringBuilder data = new StringBuilder();
for (int i = 1; i <= 100; i++) data.Append("a");
 
try
{
    conn.Open();
    //cmd.CommandText = "DROP TABLE mylobtable";
    //cmd.CommandType = CommandType.Text;
    //cmd.ExecuteNonQuery();
    //Response.Write("删除表'mylobtable'成功.
");
 
    //cmd.CommandText = "CREATE TABLE mylobtable (a varchar2(36), b clob,c blob)";
    //cmd.CommandType = CommandType.Text;
    //cmd.ExecuteNonQuery();
    //Response.Write("创建表 'mylobtable' 成功.
");
 
    cmd.CommandText = "INSERT INTO mylobtable(a,c) VALUES('" + id + "'," + ":data)";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Clear();
    OracleParameter paraBlob = new OracleParameter("data", OracleType.Blob);
    byte[] val = Encoding.UTF8.GetBytes(data.ToString());
    paraBlob.Value = val;
    cmd.Parameters.Add(paraBlob);
    cmd.ExecuteNonQuery();
    Response.Write("添加成功.
"
);
}
catch (Exception ex)
{
    Response.Write("失败." + ex);
}
finally
{
    cmd.Dispose();
    conn.Close();
    conn.Dispose();
}
从 Oracle Blob 字段读取数据
string connStr = "Data Source=am;uid=scott;pwd=1;unicode=true";
string sqlStr = "SELECT b FROM mylobtable";
 
OracleConnection conn = new OracleConnection(connStr);
OracleCommand cmd = new OracleCommand(sqlStr, conn);
OracleDataReader reader = null;
cmd.CommandText = "SELECT c FROM mylobtable";
cmd.CommandType = CommandType.Text;
 
int actual = 0;
try
{
    conn.Open();
    reader = cmd.ExecuteReader();
    Response.Write("使用 OracleLob:
"
);
    while (reader.Read())
    {
        OracleLob blob = reader.GetOracleLob(0);
        if (blob != OracleLob.Null)
        {
            BinaryReader br = new BinaryReader(blob, Encoding.UTF8);
            byte[] buffer = new byte[9];
            while ((actual = br.Read(buffer, 0, buffer.Length)) > 0)
            {
                Response.Write("Read(");
                for (int i = 0; i < buffer.Length; i++) Response.Write(buffer[i] + " ");
                Response.Write(", " + buffer.Length + ") => " + actual + "
"
);
            }
        }
        Response.Write("
"
);
    }
 
}
catch
{
    Response.Write("
读取CLOB字段失败.
"
);
}
finally
{
    conn.Close();
    conn.Dispose();
    reader.Dispose();
    cmd.Dispose();
}

 

下载 Demo

你可能感兴趣的:(.NET OracleLob 读写操作)