100分求教 一个update语句(C# 写Oracle数据库中的BLOB类型字段)

hinada99 2010-12-12 04:21:04

Oracle中 表名: TBExam
字段: ID(varchar类型) Result(BLOB类型)

TBExam中已经存在的 记录
ID Result(BLOB类型)
001 写不进来
--------------

想实现的 效果: ID 为 001 的 Result值 改成 终于,写进来了

------------
我的做法如下,但是没有成功
byte[] b = System.Text.Encoding.Unicode.GetBytes("终于,写进来了");
mySql="update TBExam set Result=b where ID=001"
OracleCommand cmd = new OracleCommand(mySql, conn);
cmd.ExecuteNonQuery();
------------
在线等高手 解答~~~要求是 C#编程实现的~~~

---------关于分值
在C#社区
http://topic.csdn.net/u/20101212/15/88d3ee91-ef10-4bbe-88a8-ebbcf89ea3bc.html
发了个100分的 求助帖, 问题尚未被解决。 没有足够分在这里开个100分了--!
如果哪位高手在 这能解决 请在
http://topic.csdn.net/u/20101212/15/88d3ee91-ef10-4bbe-88a8-ebbcf89ea3bc.html
发个回复~~ 从那里额外给分。
...全文
277 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
iihero_ 2010-12-12
  • 打赏
  • 举报
回复
这个也不难啊,给你一个通用的例子吧:

sigh, 这有什么难的吗?

给你一个完整的例子:
using System;
using System.Collections.Generic;
using System.Text;

// using oracle ado.net instead of MS ado.net for oracle
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace oracledemo
{
class TestOracle
{

OracleConnection _conn;

public void test()
{

string connstr = "User Id=scott;Password=tiger;Data Source=ora102";

try
{
_conn = new OracleConnection(connstr);
_conn.Open();
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
return;
}

// setup

StringBuilder blr;
OracleCommand cmd = new OracleCommand("", _conn);

blr = new StringBuilder();
blr.Append("DROP TABLE TBExam");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Warning: {0}", e.Message);
}

blr = new StringBuilder();
blr.Append("CREATE TABLE TBExam(ID varchar(32) PRIMARY KEY,");
blr.Append("story CLOB, Result BLOB)");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}

blr = new StringBuilder();
blr.Append("INSERT INTO TBExam values(");
blr.Append("'001',");
blr.Append("'This is a long story. Once upon a time ...',");
blr.Append("empty_blob())");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}

// update the blob value
string res = "终于,写进来了";
byte[] b = System.Text.Encoding.Unicode.GetBytes(res);

cmd.Dispose();

OracleTransaction txn = _conn.BeginTransaction();
cmd = new OracleCommand("", _conn);
try
{

cmd.CommandText = "select result from TBExam where id='001' for update";
OracleDataReader reader = cmd.ExecuteReader();
reader.Read();
OracleBlob blob = reader.GetOracleBlob(0);
blob.Write(b, 0, b.Length);
blob.Flush();
txn.Commit();
cmd.CommandText = "select result from TBExam where id='001'";
reader = cmd.ExecuteReader();
reader.Read();
blob = reader.GetOracleBlob(0);
byte[] bout = new byte[blob.Length];
blob.Read(bout, 0, bout.Length);
Console.WriteLine("blob length = " + blob.Length);
Console.WriteLine("result = " + new string(System.Text.Encoding.Unicode.GetChars(bout)) );
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
finally
{
cmd.Dispose();
_conn.Close();
_conn.Dispose();
}

}

static void Main(string[] args)
{
new TestOracle().test();
}
}
}


输出结果:
blob length = 14
result = 终于,写进来了
hinada99 2010-12-12
  • 打赏
  • 举报
回复

测试未通过~~
3x anyway

先去吃饭~~~
tangren 2010-12-12
  • 打赏
  • 举报
回复
上面少写了单引号
mySql="update TBExam set Result=rawtohex('"+"终于,写进来了"+"') where ID=001"
tangren 2010-12-12
  • 打赏
  • 举报
回复
如果存储字符,最好使用CLOB,而不是BLOB,
如果使用BLOB来存储,试试:
mySql="update TBExam set Result=rawtohex("+"终于,写进来了"+") where ID=001"

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧