Vb.net 如何使用查询将数据插入数据库

Vb.net 如何使用查询将数据插入数据库,vb.net,listview,textbox,Vb.net,Listview,Textbox,所以我有这些代码,但它得到了一个错误的语法错误。。我不知道怎么了有人能帮我吗??我是VB的新手。程序设计 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim SqlQuery = "INSERT INTO sample (FirstName,MiddleName,LastName,Gende

所以我有这些代码,但它得到了一个错误的语法错误。。我不知道怎么了有人能帮我吗??我是VB的新手。程序设计

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Try
        Dim SqlQuery = "INSERT INTO sample (FirstName,MiddleName,LastName,Gender,age,Year Level,Date of birth,Date Enrolled,Citezenship,Religion,Address,Telephone NO,Average Grade,Father,Fathers Occupation,Fathers Address,Mother,Mothers Occupation,Mothers Address,Guardian,Guardians Address,Family Income,Payed Amount,Balance) VALUES ('" & txtfname.Text & "','" & txtmname.Text & "','" & txtlname.Text & "','" & combosex.Text & "','" & comboage.Text & "','" & comboyear.Text & "','" & txtdateofbirth.Text & "','" & txtdateenrolling.Text & "','" & txtcitezen.Text & "','" & txtreligion.Text & "','" & txtstudentadd.Text & "','" & txtnumber.Text & "','" & txtgrade.Text & "','" & txtfather.Text & "','" & txtfatherocc.Text & "','" & txtfatheradd.Text & "','" & txtmother.Text & "','" & txtmotherocc.Text & "','" & txtmotheradd.Text & "','" & txtguardian.Text & "','" & txtguardianadd.Text & "','" & txtincome.Text & "','" & txtpayment.Text & "','" & TextBox2.Text & "')"
        Dim sqlcommand As New OleDbCommand
        With sqlcommand
            .CommandText = SqlQuery
            .Connection = conn
            .ExecuteNonQuery()
        End With
        MsgBox("one record succesfull added")

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

如果没有看到异常信息,很难说出到底是什么错了。。。。但我猜你是在db需要整数或日期的地方插入Sting。 您需要将数据强制转换为每个字段所需的类型

您还可以接受sql注入。尝试对数据进行参数化。

这很可能是因为您暴露了SQL注入漏洞。注意您是如何处理用户输入的:

"... VALUES ('" & txtfname.Text & "', ..."
这看起来可能只是将数据值放入查询中,但实际上是将用户输入视为可执行代码。任何带有数据库保留字符的用户输入都会导致问题。例如,如果输入值有一个单引号(例如短语
“不要使用SQL可注入代码”
),则生成的查询是:

"... VALUES ('don't use SQL injectable code', ..."
这自然会导致语法错误,因为字符串文本后有一个
t
,这是无效的SQL

不要将用户输入视为可执行代码,而是将其视为数据值。使用SQL参数将这些值添加到查询中。大概是这样的:

' replace each concatenated string with a parameter placeholder:
Dim SqlQuery = "... VALUES (@fname, ..."

Dim sqlcommand As New OleDbCommand
With sqlcommand
    .CommandText = SqlQuery
    .Connection = conn

    ' add a parameter for each placeholder:
    .Parameters.AddWithValue("@fname", txtfname.Text)

    .ExecuteNonQuery()
End With

异常消息是什么?INSERT INTO语句中的System.data.oledb.OLEDBEException:sysntax错误。System.data.oledb.oledbcommand.executeCommandTextErrorHandling(oledbHresult hr)System.data.oledb.oledbcommand.executeCommandTextForSingleResult(tagDparams dbParams,Object&ExecuteSult)System.data.oledb.oledbcommand.ExecuteCommand(命令行为,对象和执行结果)System.data.oledb.oledbcommand.ExecuterInternal(commandBehavior,string方法atsystem.oledb.oledbcommand.executenonquery()sir.。它仍然不起作用。:(我将所有的参数化了,但我仍然得到了错误。。(对不起英文)@user3242798:您看到的实际异常是什么?导致错误的查询文本是什么?这是错误System.data.oledb.OLEDBEException:INSERT INTO语句中的sysntax错误。at System.data.oledb.oledbcommand.executeCommandTextErrorHandling(oledbHresult hr)在system.data.oledb.oledbcommand.ExecuteCommand.ExecuterReaderInternal(commandBehavior,string方法atsystem.oledb.oledbcommand.ExecuteOnQuery()中的system.data.oledb.oledbcommand.ExecuterCommandTextForSingleResult(tagDparams dbParams,Object&ExecuterResult)@user3242798:看起来您的列名中有空格。您的数据库引擎允许吗?我猜不允许。您需要将这些名称限定为数据库对象。在MS SQL Server中,这意味着用方括号括起来,例如:
[出生日期]
。但我没有使用MS SQL?我使用Acces DatabesSystem.data.oledb.OLEDBEException:INSERT INTO语句中的sysntax错误。在System.data.oledb.oledbcommand.EXECUTECOMMAND和TEXTERRORSHANDING(oledbHresult hr)在System.data.oledb.oledbcommand.EXECUTECOMMAND FORSINGLERESULT(tagDparams dbParams,Object&executeResult)在System.data.Oledb.OledbCommand.ExecuteCommand(命令行为、对象和执行结果)在System.data.Oledb.OledbCommand.ExecuterReaderInternal(命令行为、字符串方法在System.Oledb.OledbCommand.ExecuteOnQuery()中)