尝试将值插入SQL Server CE数据库时出错

时间:2012-01-21 20:10:08

标签: sql vb.net insert datatable sql-server-ce

我在将值插入SQL Server Compact Edition数据库中的Account表时遇到问题。我继续收到错误消息

  

解析查询时出错。 [令牌行号= 1,令牌   line offset = 62,Token in error = s]。

这似乎是SQL Server CE的常见错误,但我尝试过的所有内容似乎都不起作用。这是代码:

Public Sub addPlan(ByVal planname As String, ByVal plannumber As String)

    Dim strSql As String = (String.Format("INSERT INTO [Account] (plan_name, plan_number) VALUES (N'{0},N'{1})", Trim(planname), Trim(plannumber)))

    Try

        Using cmdAdd As New SqlCeCommand(strSql, conn)
            cmdAdd.ExecuteNonQuery()
        End Using

    Catch ex As Exception
        MessageBox.Show(ex.ToString)

    Finally
        displayPlandt()
    End Try

End Sub

Public Sub displayPlandt()

    Dim strSql As String = "SELECT * From Account"

    Using dtadapter As New SqlCeDataAdapter(strSql, conn)

        Dim dt As New DataTable
        dtadapter.Fill(dt)
        Form1.dgridplanview.DataSource = dt

    End Using

End Sub

我的另一个问题是如何更新表单上的数据表。我在try语句的finally部分中调用此方法。但是,每次需要刷新数据库时,我都会重新查询数据库并重新填充整个数据表。有没有办法在不重新填充整个数据表的情况下做同样的事情?

谢谢!

1 个答案:

答案 0 :(得分:1)

我想你会错过最终报价。

试试这个

Dim strSql As String = (String.Format("INSERT INTO [Account] (plan_name, plan_number) VALUES (N'{0}',N'{1}')", Trim(planname), Trim(plannumber)))