下面红色的是我抄的,今天入职第二天,竟然叫我弄工作流--~!真放心。。。。
一看,我就傻眼了,结果是oracle,上吧,一会就出现问题了,我不知道用什么去声明数据库clob类型对应实体对象中的变量--~!
然后就有了下面这一串,我看的头大,于是我就用这个方法解决了--
【【【
从数据库拿到clob后:
byte[] ff = (byte[])(dt.Rows[0]["cont"]);
Literal1.Text = System.Text.Encoding.Unicode.GetString(ff);
然后就得到了你要的字符串;
往里插的时候:
byte[] expbyte = System.Text.Encoding.Unicode.GetBytes(content);
string sql = "insert into 表名(id,cont) values(SEQ_ID.Nextval,:content)";
cmd.CommandText = sql;
cmd.Parameters.Add("content", System.Data.OracleClient.OracleType.Clob, expbyte.Length);
cmd.Parameters[0].Value = expbyte;
然后就行了。
】】】
下面这个我没仔细看,等休息时候再复习。。。
.net中二进制大对象的操作(这里以clob为例)
public static void WriteLob(string table_name,string table_id,int id,int column,string content)
{
OracleConnection myCn = new OracleConnection(strConn);
OracleCommand myCmd = new OracleCommand();
try
{
myCn.Open();
}
catch(System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
OracleTransaction myTrans = myCn.BeginTransaction();
try
{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;
myCmd.CommandText = "SELECT * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
OracleDataReader reader = myCmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob clob = reader.GetOracleLob(column);//column为lob类型所在列
Encoding UTF8 = Encoding.UTF8; //使用UTF-8进行编码
byte[] buffer = UTF8.GetBytes(content);
if(buffer.Length % 2 ==0)
clob.Write(buffer, 0, buffer.Length); //这里的参数必须为偶数
else
clob.Write(buffer, 0, (buffer.Length - 1));
//clob.Position = 0;
myTrans.Commit();
}
}
catch(System.Data.OracleClient.OracleException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
public static string ReadLob(string table_name,string table_id,int id,int column)
{
OracleConnection myCn = new OracleConnection(strConn);
OracleCommand myCmd = new OracleCommand();
try
{
myCn.Open();
}
catch(System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
OracleTransaction myTrans = myCn.BeginTransaction();
try
{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;
myCmd.CommandText = "SELECT * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
OracleDataReader reader = myCmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob clob = reader.GetOracleLob(column);
int[] content = new int[(int)clob.Length];
byte[] buffer = new byte[(int)clob.Length];
for(int i = 0;i<(int)clob.Length;i++)
{
content = clob.ReadByte();
buffer = (byte)content;
}
Encoding utf8 = Encoding.UTF8;
string final = utf8.GetString(buffer);
myTrans.Commit();
return final;
}
}
catch(System.Data.OracleClient.OracleException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
经测试通过,相信对有些朋友会有帮助的。
在读记录时
for(int i = 0;i<(int)clob.Length;i++)
{
content = clob.ReadByte();
buffer = (byte)content;
}
读取太慢
用了clob.read(buffer,0,clob.length);
直接把记录读取buffer中,速度很快。