本地数据库表,未在我的表中插入记录,插入命令
问题描述:
为什么在我的本地数据库表中没有插入记录.我的代码有什么问题.
why there is no record inserted in my local database table. what''s wrong in my code.
Imports System.Data.SqlServerCe
Public con As New SqlCeConnection
Public xql As String
Public rdr As SqlCeDataReader
Public cmd As SqlCeCommand
Public conString As String = "Data Source=|DataDirectory|\Database1.sdf"
Public Sub connect()
If con.State = ConnectionState.Closed Then
con.ConnectionString = conString
con.Open()
End If
End Sub
Private Sub btnsave_Click(sender As System.Object, e As System.EventArgs) Handles btnsave.Click
connect()
xql = "INSERT INTO tblEmployee VALUES('" & txtempno.Text & "','" & txtname.Text & "','" & txtaddress.Text & "','" & txtcontact.Text & "','" & cbopos.Text & "')"
cmd = New SqlCeCommand(xql, con)
cmd.ExecuteNonQuery()
con.Dispose()
con.Close()
MessageBox.Show("Record saved!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
答
替换此:
"INSERT INTO tblEmployee VALUES('" & txtempno.Text & "','" & txtname.Text & "','" & txtaddress.Text & "','" & txtcontact.Text & "','" & cbopos.Text & "')
与:
with:
"INSERT INTO tblEmployee (FieldName1, FieldName2, FieldName3, ...) VALUES('" & txtempno.Text & "','" & txtname.Text & "','" & txtaddress.Text & "','" & txtcontact.Text & "','" & cbopos.Text & "')
您看到区别了吗?
Do you see the difference?