C# 在Oracle中写入CLOB数据

时间:2021-08-21 08:15:52

  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);
            }            
        }
    }