String Literal too long for Clob type

2019-08-30 11:25发布

I have table containing clob column. I want to save HTML data into that(c#). When i save , i get an error: String Literal too long. the string length I'm storing is 8048 characters

Can any one help me? Thanks in advance table is: CREATE TABLE tblhelp ( GRID LONG )

 private string getHTML(GridView gv)
{
    StringBuilder sb = new StringBuilder();
    StringWriter textwriter = new StringWriter(sb);
    HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter);
    gv.RenderControl(htmlwriter);
    htmlwriter.Flush();
    textwriter.Flush();
    htmlwriter.Dispose();
    textwriter.Dispose();
    return sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
    return;
}
protected void btnTest_Click(object sender, EventArgs e)
{
 //   string grid = getHTML(GridView1);
   TextBox7.Text = getHTML(GridView1);    
    OdbcConnection DbConnection1 = new OdbcConnection(con1);
    try
    {
        DbConnection1.Open();
        OdbcCommand DbCommand1 = DbConnection1.CreateCommand();
        //DbCommand1.CommandText = "UPDATE TBL_ITHELPDESK SET STATUS='"+ chkClosed.Text +"',CLOSED_BY='"+drpClosedBy.Text+"',CLOSED_ON=TO_DATE('"+txtClosedOn.Text.ToString().Trim()+"','MM-DD-YYYY')WHERE CALL_NO='" + txtCallNo.Text + "'";
        DbCommand1.CommandText = "insert into tblhelp(grid) values('" + TextBox7.Text.Replace("'", "''").Trim() + "')";
    TextBox7.Text=DbCommand1.CommandText.ToString();
        int t1 = DbCommand1.ExecuteNonQuery();
        if (t1 == 1)
        {
            DbConnection1.Close();

        }
        else
        {
        }
    }
    catch (Exception ex)
    {

    }
}

2条回答
放荡不羁爱自由
2楼-- · 2019-08-30 12:08

A string literal is limited to 4000 bytes. If you are trying to insert data into a CLOB, your code will need to use bind variables and bind the LOB data (which you should be doing for a host of other reasons related to security and performance). The error you are getting strongly implies that you are doing something like building up a SQL statement in a string that includes the literal data that you want to insert.

查看更多
唯我独甜
3楼-- · 2019-08-30 12:16

Use a command parameter:

try
{
    DbConnection1.Open();
    OdbcCommand DbCommand1 = DbConnection1.CreateCommand();
    DbCommand1.CommandText = "INSERT INTO tblhelp (grid) VALUES (?)";
    OdbcParameter param1 = new OdbcParameter("param1", OdbcType.VarChar);
    param1.Value = TextBox7.Text;
    DbCommand1.Parameters.Add(param1);
    Int32 t1 = DbCommand1.ExecuteNonQuery();
    if (t1 == 1)
    {
        DbConnection1.Close();
    }
    else
    {
    }
}
catch (Exception ex)
{
    //do something!
}
查看更多
登录 后发表回答