代码改变世界

C# 在Oracle中写入CLOB数据

2012-04-25 17:44  蓝之风  阅读(7339)  评论(0编辑  收藏  举报

  Table Schema:

  CREATE TABLE "SA"."TB_NCLOB"
    ("ID"   NUMBER(8,0) NOT NULL ENABLE,
    "NAME" VARCHAR2(20 BYTE),
    "TEXT" CLOB)

  C# 代码

  

public static void InsertWithLob()
    {
        if (mConnection != null && mConnection.State == ConnectionState.Open)
        {           
            try
            {
                string sqlText="insert into tb_nclob(id,name,text)values(1,'test',:text)";
                using (OracleCommand cmd=new OracleCommand(sqlText,mConnection))
                {
                    OracleParameter oracleParameter = new OracleParameter("text", OracleDbType.Clob);
                    oracleParameter.Value = "this is my test";
                    cmd.Parameters.Add(oracleParameter);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (OracleException ex)
            {
                throw new Exception(ex.Message);
            }            
        }
    }