在'ExecuteNonQuery'中收到错误

时间:2013-09-02 07:03:56

标签: vb.net executenonquery

错误为'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

2 个答案:

答案 0 :(得分: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并非所有这些都在一个

答案 1 :(得分: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)