我正在使用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
答案 0 :(得分:1)
你最后错过了一个括号
& dtpWhen.Text & "'", con)
应该是
& dtpWhen.Text & "')", con)
答案 1 :(得分:1)
不使用SqlCeDataAdapter,而是使用SqlCeCommand对象。在创建它之后,实际使用它(在它上面调用ExecuteNonQuery)。并删除Try,Catch Ex as Exception和End Try行,这样,如果发生错误,您实际上会看到它。
这就是我从30秒钟看到的东西。
修改的
您还应该考虑使用parameters而不是将INSERT语句连接在一起。
答案 2 :(得分:1)
您正在构建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()