【问题标题】:da.fill incorrect syntax near ')'da.fill ')' 附近的错误语法
【发布时间】:2014-10-06 07:19:40
【问题描述】:

我的线路有问题

da.Fill(ds, "Employee")

我没有任何线索可以解决这个问题。有人可以帮忙吗?

这是我的实际代码:

 Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
    Dim da As New SqlClient.SqlDataAdapter
    Dim ds As New DataSet
    Dim dt As New DataTable


    If txtssn.Text = "" Then
        MsgBox("Please input SSN.", MsgBoxStyle.Exclamation, "Company Records - Employee")
    Else
        con.Open()
        Dim cmd As New SqlCommand("SELECT * FROM [Employee] WHERE [Ssn] = '" & Trim(Me.txtssn.Text) & "')", con)

        da.SelectCommand = cmd

        da.Fill(ds, "Employee")
        dt = ds.Tables("Employee")

        If (dt.Rows.Count > 0) Then
            Me.txtfname.Text = dt.Rows(0).Item(1)
            Me.txtmi.Text = dt.Rows(0).Item(2)
            Me.txtlname.Text = dt.Rows(0).Item(3)
            Me.dtpbdate.Text = dt.Rows(0).Item(5)
            Me.txtaddress.Text = dt.Rows(0).Item(6)
            Me.cmbsex.Text = dt.Rows(0).Item(7)
            Me.txtsalary.Text = dt.Rows(0).Item(8)
            Me.cmbsuperssn.Text = dt.Rows(0).Item(9)
            'Me.cmbdept.Text =
            btnedit.Enabled = True
            btndelete.Enabled = True
            editable()

        Else
            MsgBox("Record Not Found", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Company Records - Employee")
        End If

        con.Close()
    End If

【问题讨论】:

  • 我心中的魔鬼说:键入';DROP TABLE Employee;--,但我更愿意说:阅读有关Sql Injection的信息

标签: vb.net syntax-error


【解决方案1】:

删除右括号,因为那是 SELECT 而不是 INSERT

"SELECT * FROM [Employee] WHERE [Ssn] = '" & Trim(Me.txtssn.Text) & "'"

但是,我总是使用 sql-parameters 来防止 sql-injection。

Using con As New SqlConnection("ConenctionString")
    Using da As New SqlDataAdapter("SELECT * FROM [Employee] WHERE [Ssn] = @SSN", con)
        da.SelectCommand.Parameters.Add("@SSN", SqlDbType.VarChar).Value = txtssn.Text
        da.Fill(ds, "Employee")
    End Using
End Using

【讨论】:

    【解决方案2】:

    从您的 SQL 语句中删除结尾的 )

    "SELECT * FROM [Employee] WHERE [Ssn] = '" & Trim(Me.txtssn.Text) & "'"
    

    也看看你为什么shouldn't be doing it in the first place

    【讨论】:

      【解决方案3】:

      SQLstatement 附近存在语法错误,因此您需要删除不需要的 ( 以使该语句可行。

       Dim cmd As New SqlCommand("SELECT * FROM [Employee] WHERE [Ssn] = '" & Trim(Me.txtssn.Text) & "'", con)
      

      【讨论】:

        猜你喜欢
        • 2013-11-02
        • 1970-01-01
        • 2013-06-22
        • 2017-08-31
        • 2023-03-03
        • 2016-02-16
        • 2018-08-21
        • 2017-01-12
        • 2014-03-12
        相关资源
        最近更新 更多