请问,我窗口中有一个richTextBox控件。我想将这个控件中用户输入保存到数据库中,代码如下:SqlCommand cmd = new SqlCommand("insert into tb_KnowledgeLibrary(knowledge) values('"+ richTextBox1.Text.Trim() + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
问题是如果用户输入的内容中本身就有很多单引号‘,那么这段代码运行的时候就出错了。请问有什么比较好的方法处理么?谢谢。

解决方案 »

  1.   


    string str=string.Format("insert into tb_KnowledgeLibrary(knowledge) values {0}",richTextBox1.Text.Trim());
    SqlCommand cmd = new SqlCommand(str,conn);
      cmd.ExecuteNonQuery();
      conn.Close();
      

  2.   


    richTextBox1.Text.Trim().Replace("'","''");
      

  3.   

    用参数传值啊
    SqlCommand cmd=conn.CreateCommand();
    cmd.CommandText="insert into tb_KnowledgeLibrary(knowledge) values(@knowledge)";
    cmd.Parameters.Add(new SqlParameter("knowledge",richTextBox1.Text));