0

错误是'Sqlexception was unhanded ' There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = * ]

Dim con As SqlCeConnection = New SqlCeConnection("Data Source = C:\Users\vineet\Documents\Visual Studio 2010\Projects\sqlProject\sqlProject\studentDB.sdf")
Dim dcmd As SqlCeCommand
Dim da As SqlCeDataAdapter
Dim ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '   ShowData()
End Sub

Public Sub ShowData()
    dcmd = New SqlCeCommand("Select * FROM studentTable", con)
    If con.State = ConnectionState.Closed Then con.Open()
    'now we will populate the dataset with the help of data adapter
    da = New SqlCeDataAdapter(dcmd)
    ds = New DataSet()
    da.Fill(ds, "MyTable")
    DataGridStudents.DataSource = ds.Tables("MyTable").DefaultView
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    ShowData()
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    dcmd = New SqlCeCommand("Delete * From studentTable Where name=@name", con)
    dcmd.Parameters.AddWithValue("@name", TextBox6.Text)
    If con.State = ConnectionState.Closed Then con.Open()
    dcmd.ExecuteNonQuery()
    ShowData()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    dcmd = New SqlCeCommand("Insert Into studentTable(ID,name,address,mobile,email) Values(@fID,@fname,@faddress,@fmobile,@femail)")
    dcmd.Parameters.AddWithValue("@ID", TextBox1.Text)
    dcmd.Parameters.AddWithValue("@fname", TextBox1.Text)
    dcmd.Parameters.AddWithValue("@faddress", TextBox1.Text)
    dcmd.Parameters.AddWithValue("@fmobile", TextBox1.Text)
    dcmd.Parameters.AddWithValue("@femail", TextBox1.Text)
    If con.State = ConnectionState.Closed Then con.Open()
    dcmd.ExecuteNonQuery()
    ShowData()
End Sub
End Class
4

2 回答 2

2

您正在传递TextBox1.Text每个参数值。

dcmd.Parameters.AddWithValue("@ID", TextBox1.Text)
dcmd.Parameters.AddWithValue("@fname", TextBox1.Text)
dcmd.Parameters.AddWithValue("@faddress", TextBox1.Text)
dcmd.Parameters.AddWithValue("@fmobile", TextBox1.Text)
dcmd.Parameters.AddWithValue("@femail", TextBox1.Text)

从发布的代码中,我不确定需要映射到每条数据的实际 UI 控件的名称是什么,但我很确定TextBox1.Text不是所有这些都合二为一。

于 2013-09-02T07:49:04.897 回答
1

它应该是 ..

dcmd.Parameters.AddWithValue("@fID", TextBox1.Text)

它应该是..

dcmd = New SqlCeCommand("Insert Into studentTable(ID,name,address,mobile,email) Values(@fID,@fname,@faddress,@fmobile,@femail)", con)
于 2013-09-02T07:08:35.813 回答