向ACCESS库中保存html的时候如果有'号就会出现以下错误:语法错误 (操作符丢失) 在查询表达式 ''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>一起去看书_177ks_书籍列表页</title> <title>一起去看书____</title> <met' 中。

解决方案 »

  1.   

    肯定不是字段小的问题,字段小的时候ACCESS会提示你截断字符串错误,而不是语法错误上述错误是错在保存长文本时竟然使用未格式化的 SQL 字串正确的保存长文本的方法应该是用 ADO或者 ADO.NET对象来保存
    比如 ADODB.RECORDSET.OPEN 以后再UPDATE你必定要用 SQL 来保存的话请格式化字串,比如update table set fieldname ='  ''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  '看见第一个单引号了么?要重复两遍,按你编写代码的环境不同,可能其中的双引号也全部要重复两遍 ,第一个引号是作为转义字符用的
      

  2.   

    刚才的楼上说的是"net_lover(孟子E章) ( ) 信誉:140 "这个楼..
      

  3.   

    那就是你插入得有非法字符了。转换下,在存入数据库,不过字段最好用备注类型得。 #region 转换非法字符
    /// <summary>
    /// 转换非法字符
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns> public static string strEncode(string str)
    {
    if(str!=null)
    {
    System.Text.StringBuilder newstr=new System.Text.StringBuilder();
    string xChar="";

    int l=str.Length;
    for(int i=0;i<l;i++)
    {
    xChar=str.Substring(i,1);
    switch(xChar)
    {

    case("\'"):
    newstr.Append("‘");
    break;
    case("\""):
    newstr.Append("“");
    break;
    case("("):
    newstr.Append("(");
    break;
    case(")"):
    newstr.Append(")");
    break;

    case(":"):
    newstr.Append(":");
    break;
    case("<"):
    newstr.Append("<");
    break;
    case(">"):
    newstr.Append(">");
    break;
    default:
    newstr.Append(xChar);
    break;

    }

    return newstr.ToString();
    }
    else
    return null;
    } #endregion
    #region 转换非法字符(文本框格式内容转换)
    /// <summary>
    /// 转换非法字符
    /// </summary>
    /// <param name="str"></param>
    /// <param name="style">Text</param>
    /// <returns></returns> public static string strEncode(string str,string style)
    {
    if(str!=null)
    { System.Text.StringBuilder newstr=new System.Text.StringBuilder();
    string xChar="";
    int l=str.Length;

    if(style=="xml")
    {

    for(int i=0;i<l;i++)
    {
    xChar=str.Substring(i,1);
    switch(xChar)
    {

    case("\'"):
    newstr.Append("‘");
    break;
    case("\""):
    newstr.Append("“");
    break;
    case("("):
    newstr.Append("(");
    break;
    case(")"):
    newstr.Append(")");
    break;
    // case(":"):
    // newstr.Append(":");
    // break;
    case("<"):
    newstr.Append("&lt;");
    break;
    case(">"):
    newstr.Append("&gt;");
    break;
    /*  case(" "):
    newstr.Append("&#160;");
    break;

    case("\n"):

     * newstr.Append("<br>");
    break;
    case("\r"):
    newstr.Append("<br>");
    break; */ default:
    newstr.Append(xChar);
    break;

    }
    }

    }
    else
    {
    for(int i=0;i<l;i++)
    {
    xChar=str.Substring(i,1);
    switch(xChar)
    {

    case("\'"):
    newstr.Append("‘");
    break;
    case("\""):
    newstr.Append("“");
    break;
    case("("):
    newstr.Append("(");
    break;
    case(")"):
    newstr.Append(")");
    break;

    case(":"):
    newstr.Append(":");
    break;
    case(" "):
    newstr.Append("&nbsp;");
    break;
    case("<"):
    newstr.Append("&lt;");
    break;
    case(">"):
    newstr.Append("&gt;");
    break;
    case("\n"):
    newstr.Append("<br>");
    break;
    case("\r"):
    newstr.Append("<br>");
    break; default:
    newstr.Append(xChar);
    break;

    }
    }


    return newstr.ToString();
    }
    else
    return null;
    } #endregion #region 反向转换非法字符
    /// <summary>
    /// 反向转换非法字符
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns> public static string strUNEncode(string str)
    {
    str=str.Replace("“","\"");
    str=str.Replace("‘","\'");
    str=str.Replace(")",")");
    str=str.Replace("(","(");
    return str;

    } #endregion
    #region 反向转换非法字符(文本框内容转换)
    /// <summary>
    /// 反向转换非法字符
    /// </summary>
    /// <param name="str"></param>
    /// <param name="style">text为文本框格式的</param>
    /// <returns></returns> public static string strUNEncode(string str,string style)
    {
    str=str.Replace("“","\"");
    str=str.Replace("‘","\'");
    str=str.Replace(")",")");
    str=str.Replace("(","(");
    str=str.Replace("&gt;",">");
    str=str.Replace("&lt;","<");
    str=str.Replace("&nbsp;"," ");
    str=str.Replace("<br>","\n"); return str;

    }

    }
    }