简体   繁体   English

Richtextbox进入Vb.net中的Access

[英]Richtextbox into Access in Vb.net

I am trying to save richtextbox content to Microsoft Access in a vb 2008. 我正在尝试在vb 2008中将Richtextbox内容保存到Microsoft Access。

I have uploaded a rtf file to richtextbox. 我已将rtf文件上传到richtextbox。 Now richtextbox have tables, paragraphs. 现在richtextbox具有表,段落。

I tried richtextbox1.rtf, but when i am saving i am getting error as 我尝试了richtextbox1.rtf,但是当我保存时却收到错误消息

"Syntax error (missing operator) in query expression ''{\\rtf1\\fbidis\\ansi\\ansicpg1252\\deff0\\deflang1033\\deflangfe1033{\\fonttbl{\\f0\\fswiss\\fprq2\\fcharset0 Calibri;}{\\f1\\fnil\\fcharset0 Calibri;}} \\viewkind4\\uc1\\pard\\ltrpar\\sa200\\sl276\\slmult1\\f0\\fs22 Estonia\\rquote s economic growth c'." “查询表达式” {\\ rtf1 \\ fbidis \\ ansi \\ ansicpg1252 \\ deff0 \\ deflang1033 \\ deflangfe1033 {\\ fonttbl {\\ f0 \\ fswiss \\ fprq2 \\ fcharset0 Calibri;} {\\ f1 \\ fnil \\ fcharset0 Calibri中的语法错误(缺少运算符) ;}} \\ viewkind4 \\ uc1 \\ pard \\ ltrpar \\ sa200 \\ sl276 \\ slmult1 \\ f0 \\ fs22爱沙尼亚\\ rquote的经济增长c'。”

Any suggestions. 有什么建议么。

Here is my code 这是我的代码

    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty

    connection.Open()
    cmd.Connection = connection
    cmd.CommandType = CommandType.Text


    sSQL = "INSERT INTO assessment (a)"
    sSQL = sSQL & " values('" & t1.rtf & "')"
    cmd.CommandText = sSQL
    cmd.ExecuteNonQuery()

    MsgBox("Data has been saved")

Your rtf information probably contains a single quote somewhere, which messes up your string and the database engine can't figure out what is the text anymore. 您的rtf信息可能在某处包含一个单引号,这会弄乱您的字符串,并且数据库引擎无法再弄清楚什么是文本。

You need to use parameters: 您需要使用参数:

sSQL = "INSERT INTO assessment (a) values (@a)"

cmd.Parameters.AddWithValue("@a", t1.rtf)

BTW, always use parameters to avoid SQL injection. 顺便说一句,请始终使用参数来避免SQL注入。 Someone can write somethine like ';DROP TABLE assessment in your textbox, and guess what, your table would mysteriously disappear. 有人可以在文本框中编写诸如';DROP TABLE assessment';DROP TABLE assessment东西,然后猜测会导致您的表神秘地消失。 Parameters would protect your from that. 参数可以保护您免受此伤害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM