C# 长度超过40万的文本,保存到oracle clob字段中去
2011-04-05 01:00
513 查看
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request.Files.Count == 0)
{
return;
}
HttpPostedFile postedFile = this.Request.Files[0];
int ContentLength = postedFile.ContentLength;
if (ContentLength == 0)
return;
byte[] bytes = new byte[ContentLength];
//把上传的文件中的XML文本读出到string
postedFile.InputStream.Read(bytes, 0, ContentLength);
string tempStr = System.Text.Encoding.Default.GetString(bytes);
//获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
//因为OracleLob Clob存储是以UTF-16的合格,如果不转换就会保存成乱码
bytes = System.Text.Encoding.Unicode.GetBytes(tempStr);
ContentLength = bytes.Length;
string connString = "Data Source=owen;User Id=userid;Password=password;Integrated Security=no;";
System.Data.OracleClient.OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
OracleCommand command = new OracleCommand();
command.Transaction = trans;
command.Connection = conn;
command.CommandType = CommandType.Text;
//获取临时clob
command.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :templob := xx; end;";
command.Parameters.Add(new OracleParameter("templob", OracleType.Clob)).Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
OracleLob tmpclob = (OracleLob)command.Parameters[0].Value;
//end 获取临时clob
tmpclob.Write(bytes, 0, ContentLength);
tmpclob.Position = 0;
trans.Commit();
command.Parameters.Clear();
command.CommandText = "pro_test1";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new OracleParameter("name", Guid.NewGuid().ToString()));
command.Parameters.Add(new OracleParameter("clob1", OracleType.Clob, ContentLength,
ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Current, tmpclob));
command.ExecuteNonQuery();
this.Response.Write("insert OK");
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
if (conn != null)
conn.Close();
}
}
数据库内容
create table TEST1
(
NAME VARCHAR2(100),
UPDATED_TIME DATE default sysdate,
ROWS_COUNT NUMBER default 0,
CLOB1 CLOB
)
/
create or replace procedure pro_test1(name in varchar2,clob1 in clob)
as
theXmlParser dbms_xmlparser.Parser;
theXmlDoc dbms_xmldom.DOMDocument;
theXmlNodeList dbms_xmldom.DOMNodeList;
rowsCount number(10);
begin
theXmlParser := dbms_xmlparser.newParser;
dbms_xmlparser.parseclob(theXmlParser, clob1);
theXmlDoc := dbms_xmlparser.getDocument(theXmlParser);
theXmlNodeList := dbms_xslprocessor.selectNodes(dbms_xmldom.makeNode(theXmlDoc),'/table/row');
rowsCount := dbms_xmldom.getLength(theXmlNodeList);
insert into test1 (name,clob1,rows_count) values (name,clob1,rowsCount);
commit;
end pro_test1;
/
{
if (this.Request.Files.Count == 0)
{
return;
}
HttpPostedFile postedFile = this.Request.Files[0];
int ContentLength = postedFile.ContentLength;
if (ContentLength == 0)
return;
byte[] bytes = new byte[ContentLength];
//把上传的文件中的XML文本读出到string
postedFile.InputStream.Read(bytes, 0, ContentLength);
string tempStr = System.Text.Encoding.Default.GetString(bytes);
//获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
//因为OracleLob Clob存储是以UTF-16的合格,如果不转换就会保存成乱码
bytes = System.Text.Encoding.Unicode.GetBytes(tempStr);
ContentLength = bytes.Length;
string connString = "Data Source=owen;User Id=userid;Password=password;Integrated Security=no;";
System.Data.OracleClient.OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
OracleCommand command = new OracleCommand();
command.Transaction = trans;
command.Connection = conn;
command.CommandType = CommandType.Text;
//获取临时clob
command.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :templob := xx; end;";
command.Parameters.Add(new OracleParameter("templob", OracleType.Clob)).Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
OracleLob tmpclob = (OracleLob)command.Parameters[0].Value;
//end 获取临时clob
tmpclob.Write(bytes, 0, ContentLength);
tmpclob.Position = 0;
trans.Commit();
command.Parameters.Clear();
command.CommandText = "pro_test1";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new OracleParameter("name", Guid.NewGuid().ToString()));
command.Parameters.Add(new OracleParameter("clob1", OracleType.Clob, ContentLength,
ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Current, tmpclob));
command.ExecuteNonQuery();
this.Response.Write("insert OK");
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
if (conn != null)
conn.Close();
}
}
数据库内容
create table TEST1
(
NAME VARCHAR2(100),
UPDATED_TIME DATE default sysdate,
ROWS_COUNT NUMBER default 0,
CLOB1 CLOB
)
/
create or replace procedure pro_test1(name in varchar2,clob1 in clob)
as
theXmlParser dbms_xmlparser.Parser;
theXmlDoc dbms_xmldom.DOMDocument;
theXmlNodeList dbms_xmldom.DOMNodeList;
rowsCount number(10);
begin
theXmlParser := dbms_xmlparser.newParser;
dbms_xmlparser.parseclob(theXmlParser, clob1);
theXmlDoc := dbms_xmlparser.getDocument(theXmlParser);
theXmlNodeList := dbms_xslprocessor.selectNodes(dbms_xmldom.makeNode(theXmlDoc),'/table/row');
rowsCount := dbms_xmldom.getLength(theXmlNodeList);
insert into test1 (name,clob1,rows_count) values (name,clob1,rowsCount);
commit;
end pro_test1;
/
相关文章推荐
- java 将长度很长的字符串(巨大字符串超过4000字节)插入oracle的clob字段时会报错的解决方案
- oracle中修改已有字段数据类型为clob的方法以及ssh框架中如何保存clob字段
- 将 C# 的图像对象保存到 Oracle BLOB 字段中
- 用C#将图片保存至Oracle BLOB字段中的方法
- oracle 实际值超过数据库某个字段指定长度报错解决
- C#更新Oracle CLOB字段
- 在Oracle中如何保存长文本??一个表中不能有两个CLOB或LONG或BLOB?
- clob字段只能写入4k内容,超过后报null at org.apache.openjpa.jdbc.sql.OracleDictionary.putString
- NHibernate中Oracle字段类型Clob数据长度过大问题
- 【转】Oracle + PHP Cookbook(php oracle clob 长度超过4000如何写入)
- 【转】Oracle + PHP Cookbook(php oracle clob 长度超过4000如何写入)
- C# 结合 oracle处理clob字段文件时出错
- C#中去的Oracle中的表字段类型,长度,精度
- c# winform 读取oracle中blob字段的图片并且显示到pictureBox里,保存进库
- Hibernate保存Oracle的Clob字段的解决方案
- [急]Oracle Clob字段插入数据库时报:字符串长度过长
- 请教oracle的CLOB字段的最大长度?
- oracle 实际值超过数据库某个字段指定长度报错解决
- Oracle Clob字段保存时提示字符串过长
- oracle CLOB类型超过4000个字段sql插入方法