SQL语句不会插入DB.sdf

发布于 2024-09-18 09:30:48 字数 1439 浏览 3 评论 0原文

我正在使用 Visual Studio 2008 并已正确连接数据库,因为我已经完成了正常工作的登录,尽管当我尝试插入在文本框中提交的信息到不同的表时,在我结束程序检查它后它不会输入仍然没有数据。有什么想法吗?

    Dim con As SqlCeConnection = New SqlCeConnection("Data Source=NESdb.sdf")        
    Dim myDA As SqlCeDataAdapter
    Dim myDataSet As DataSet
    Dim dt As New DataTable()        

    'Connect to database'
    con.Open()
    'Attempt to retrieve data'
    Try ' Select username and password that match'
        Dim cmd As SqlCeDataAdapter = New SqlCeDataAdapter("INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town, PostCode, Telephone, Mob, Email, VehicleType, RegNo, Year, Make, Model, V5, Collected, CollectionDate)" + "VALUES('" & txtFname.Text & "', '" & txtLname.Text & "', '" & txtAdd1.Text & "', '" & txtAdd2.Text & "', '" & txtTown.Text & "', '" & txtPostCode.Text & "', '" & txtTelephone.Text & "', '" & txtMob.Text & "', '" & txtEmail.Text & "', '" & comboVehicleType.Text & "', '" & txtReg.Text & "', '" & comboYear.Text & "', '" & comboMake.Text & "', '" & txtModel.Text & "', '" & chkV5.Text & "', '" & chkCollected.Text & "', '" & dtpWhen.Text & "')", con)
        'Catch errors'
    Catch ex As Exception
    End Try
    'Close connection to database'
    If con.State <> ConnectionState.Closed Then
        con.Close()
    End If

I am using Visual Studio 2008 and have connected a database correctly as I have done a login that works fine, although when I try to insert information submitted in the text boxes a different table, it doesn't enter after I end program to check it still has no data in. Any ideas?

    Dim con As SqlCeConnection = New SqlCeConnection("Data Source=NESdb.sdf")        
    Dim myDA As SqlCeDataAdapter
    Dim myDataSet As DataSet
    Dim dt As New DataTable()        

    'Connect to database'
    con.Open()
    'Attempt to retrieve data'
    Try ' Select username and password that match'
        Dim cmd As SqlCeDataAdapter = New SqlCeDataAdapter("INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town, PostCode, Telephone, Mob, Email, VehicleType, RegNo, Year, Make, Model, V5, Collected, CollectionDate)" + "VALUES('" & txtFname.Text & "', '" & txtLname.Text & "', '" & txtAdd1.Text & "', '" & txtAdd2.Text & "', '" & txtTown.Text & "', '" & txtPostCode.Text & "', '" & txtTelephone.Text & "', '" & txtMob.Text & "', '" & txtEmail.Text & "', '" & comboVehicleType.Text & "', '" & txtReg.Text & "', '" & comboYear.Text & "', '" & comboMake.Text & "', '" & txtModel.Text & "', '" & chkV5.Text & "', '" & chkCollected.Text & "', '" & dtpWhen.Text & "')", con)
        'Catch errors'
    Catch ex As Exception
    End Try
    'Close connection to database'
    If con.State <> ConnectionState.Closed Then
        con.Close()
    End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

潦草背影 2024-09-25 09:30:48

您正在构建 cmd 对象,但不执行它。

建议插入时丢弃适配器。试试这个:

/*snipped values for brevity.*/
Dim insertSql As String = "INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town) VALUES(@FName, @LName, @Add1, @Add2, @Town)" 

conn.Open()
Dim cmd As New SqlCeCommand(insertSql, conn)
cmd.Parameters.Add(New SqlCeParameter("@FName", txtFirstName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@LName", txtLastName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@Add1", txtAdd1.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@Add2", txtAdd2.Text.Trim()))

cmd.ExecuteNonQuery()
conn.Close()

You're building up the cmd object, but you don't execute it.

Suggest ditch the Adapter when inserting. Try this instead:

/*snipped values for brevity.*/
Dim insertSql As String = "INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town) VALUES(@FName, @LName, @Add1, @Add2, @Town)" 

conn.Open()
Dim cmd As New SqlCeCommand(insertSql, conn)
cmd.Parameters.Add(New SqlCeParameter("@FName", txtFirstName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@LName", txtLastName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@Add1", txtAdd1.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("@Add2", txtAdd2.Text.Trim()))

cmd.ExecuteNonQuery()
conn.Close()
去了角落 2024-09-25 09:30:48

您在末尾缺少括号

& dtpWhen.Text 和"'", con)

应该是

& dtpWhen.Text 和“')”,con)

You are missing a bracket at the end

& dtpWhen.Text & "'", con)

should be

& dtpWhen.Text & "')", con)

扛刀软妹 2024-09-25 09:30:48

而不是使用 SqlCeDataAdapter,使用 SqlCeCommand 对象。创建后,实际使用它(调用 ExecuteNonQuery )。并删除 Try、Catch Ex as Exception 和 End Try 行,以便如果发生错误,您实际上会看到它。

这就是我从 30 秒的观察中所看到的。

编辑

您还应该查看使用 参数 而不是将 INSERT 语句连接在一起。

Instead of using a SqlCeDataAdapter, use a SqlCeCommand object. And after creating it, actually use it (call ExecuteNonQuery on it). And remove the Try, Catch Ex as Exception and End Try lines so that, if an error occurs, you'll actually see it.

That's what I can see from 30 seconds of looking.

Edit

You should also look at using parameters rather than concatenating the INSERT statement together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文