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

hinada99 2010-12-12 03:53:54

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#编程实现的~~~

...全文
261 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyming4114 2010-12-12
  • 打赏
  • 举报
回复
数据连接问题:例如:用参数法,定义成如下的语句:
sql = "update tabname set fd3 = @FD3 where fd1 = @fd2 and fd2 = @fd2
SqlParameter[] parameters =
{
new SqlParameter("FD1",SqlDbType.Int),
new SqlParameter("FD2",SqlDbType.DateTime),
new SqlParameter("FD3",SqlDbType.Int),
};
whrspsoft3723 2010-12-12
  • 打赏
  • 举报
回复
用参数法,定义成如下的语句:
sql = "update tabname set fd3 = @FD3 where fd1 = @fd2 and fd2 = @fd2
SqlParameter[] parameters =
{
new SqlParameter("FD1",SqlDbType.Int),
new SqlParameter("FD2",SqlDbType.DateTime),
new SqlParameter("FD3",SqlDbType.Int),
};

parameters[0].Value = uKey;

parameters[1].Value = cTime;

parameters[2].Value = cTimes;

//调用数据操作类DataOper(自己写的)

int iCount = db.ExecSQL("Insert_Calender", parameters);

以上是sqlserver的语法,oracle有类似 的定义。

qldsrx 2010-12-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 hinada99 的回复:]
cmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
求解释
[/Quote]
在Oracle数据库中为blob数据类型申请一段临时空间,返回的变量即为临时空间地址,然后C#中往该地址写入字节数据,最后交给更新语句的参数用于插入或者更新。
jerysab 2010-12-12
  • 打赏
  • 举报
回复
bud /.............
iihero_ 2010-12-12
  • 打赏
  • 举报
回复
事实上,在oracle的odp.net目录中有一大堆例子,楼主不妨把例子中的代码改改,直接就可以用了。
我上边的代码经过完整测试。
hinada99 2010-12-12
  • 打赏
  • 举报
回复
cmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
求解释
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 = 终于,写进来了
wangderong8389945 2010-12-12
  • 打赏
  • 举报
回复
mySql="update TBExam set Result=b where ID=001"
把这句改改
hinada99 2010-12-12
  • 打赏
  • 举报
回复
人生如梦兄呀~~
你转的那个帖子 是 CLOB的呀~~
CLOB跟BLOB 有类似

但是。。。。
qldsrx 2010-12-12
  • 打赏
  • 举报
回复
我给你的是重组的代码,从本人的通用函数中重组的,通用函数本身一直在用,没问题,所以如果你测试中有问题,也不会是什么大问题,根据提示自己都可以修改。
hinada99 2010-12-12
  • 打赏
  • 举报
回复
青龙白虎兄呀~~
我测了 3个小时的 各种代码了~~~

累呀

你测过自己的 答复没
qldsrx 2010-12-12
  • 打赏
  • 举报
回复
请自行测试如下代码,conn需要外部声明,为Oracle连接。

using (OracleCommand cmd = new OracleCommand())
{
OracleConnection connection = conn as OracleConnection;
OracleTransaction transaction = connection.BeginTransaction();
cmd.Connection = connection;
cmd.Transaction = transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
cmd.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
OracleLob tempLob = (OracleLob)cmd.Parameters[0].Value;
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
byte[] tempbuff = System.Text.Encoding.Unicode.GetBytes("终于,写进来了");
tempLob.Write(tempbuff, 0, tempbuff.Length);
tempLob.EndBatch();
OracleParameter p1 = new OracleParameter("b",tempLob);
cmd.Parameters.Clear();
cmd.Parameters.Add(p1);
cmd.CommandText = "update TBExam set Result=:b where ID=001";
cmd.ExecuteNonQuery();
tempLob.Dispose();
transaction.Commit();
}
hinada99 2010-12-12
  • 打赏
  • 举报
回复
在线等用C#写过Oracle中BLOB类型字段的高手~
hinada99 2010-12-12
  • 打赏
  • 举报
回复
主要是 Result这个字段呀 存的是评语~~
“某某某学习成绩优秀,表现良好,在班上起着模范带头作用。。。。。。。。”1000个汉字
太长了 不能VarChar来存
hinada99 2010-12-12
  • 打赏
  • 举报
回复
就是
BLOB类型的字段怎样 update呢?
foxd 2010-12-12
  • 打赏
  • 举报
回复
不懂,帮顶
华夏小卒 2010-12-12
  • 打赏
  • 举报
回复
类型不匹配?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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