Home  >  Article  >  Database  >  Oracle中对超长内容的处理

Oracle中对超长内容的处理

WBOY
WBOYOriginal
2016-06-07 16:56:081267browse

原来采用了Varchar2的类型来存储,但发现后修改为Clob类型的也出现同样的问题,而且发现日志的错误是:System.Data.OracleCl

  本人在做一个新闻内容的模块的时候,发现如果内容在4K以上的字符串会出错,得到的内容会是乱码(也就是被自动截断),如果小于4K,那么就不会有问题。

  原来采用了Varchar2的类型来存储,但发现后修改为Clob类型的也出现同样的问题,而且发现日志的错误是:System.Data.OracleClient.OracleException: ORA-01461: can bind a LONG value only for insert into a LONG column。

  由于Clob类型是可以存放很大类型的文本数据的,不会是数据库字段容量不够,因此检查插入和更新的C#代码,发现原来的代码是这样的:

  代码

  public bool Insert(Hashtable recordField, string targetTable, DbTransaction trans)

  {

  bool result = false;

  string fields = ""; // 字段名

  string vals = ""; // 字段值

  if ( recordField == null || recordField.Count

  {

  return result;

  }

  List paramList = new List();

  IEnumerator eKeys = recordField.Keys.GetEnumerator();

  while ( eKeys.MoveNext() )

  {

  string field = eKeys.Current.ToString();

  fields += field + ",";

  if (!string.IsNullOrEmpty(seqField) && !string.IsNullOrEmpty(seqName)

  && (field.ToUpper() == seqField.ToUpper()))

  {

  vals += string.Format("{0}.NextVal,", seqName);

  }

  else

  {

  vals += string.Format(":{0},", field);

  object val = recordField[eKeys.Current.ToString()];

  paramList.Add(new OracleParameter(":" + field, val));

  }

  }

  fields = fields.Trim(',');//除去前后的逗号

  vals = vals.Trim(',');//除去前后的逗号

  string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", targetTable, fields, vals);

  Database db = DatabaseFactory.CreateDatabase();

  DbCommand command = db.GetSqlStringCommand(sql);

  command.Parameters.AddRange(paramList.ToArray());

  if ( trans != null )

  {

  result = db.ExecuteNonQuery(command, trans) > 0;

  }

  else

  {

  result = db.ExecuteNonQuery(command) > 0;

  }

  return result;

  }

  重要的地方就是我使用了该行代码:

  paramList.Add(new OracleParameter(":" + field, val));

  猜想可能是由于这行代码的问题导致,,因此修改参数化的Oracle参数变量代码为另外一个种指定对象类型的方式:

  OracleParameter a = new OracleParameter(":" + field, OracleType.Clob, val.ToString().Length);

  a.Value = val;

  paramList.Add(a);

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn