da。填写错误的语法')'

时间:2014-10-06 07:19:40

标签: vb.net syntax-error

我遇到了问题

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

3 个答案:

答案 0 :(得分:2)

删除关闭的parantheses,因为那是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

答案 1 :(得分:1)

从SQL语句中删除尾随的)

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

另请参阅原因shouldn't be doing it in the first place

答案 2 :(得分:1)

他们在SQL语句附近出现语法错误,因此您需要删除不需要的(才能使此语句可用。

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