我是Visual Basic数据库的新手我使用.sdf
连接到数据库
我在运行此功能时在visual studio 2010
中收到以下错误。
解析查询时出错。 [令牌行号= 1,令牌 line offset = 21,令牌错误= - ]
任何人都可以告诉我如何修复此错误。
Private Sub Save_Record()
Dim con As SqlCeConnection = New SqlCeConnection
Dim cmd As New SqlCeCommand
Dim sSQL As String = String.Empty
con.ConnectionString = Get_Constring()
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
Try
If Me.txtFirstName.Tag = 0 Then
sSQL = "INSERT INTO patients-2015 ( first_name, last_name, gender, phone_number, age, address, height, standing_bp, heart_rate, notes, reffered, disease, sub_diseases, registered_date, updated_date, prescribed_medicines)"
sSQL = sSQL & " VALUES ( @first_name, @last_name, @gender, @phone_number, @age, @address, @height, @standing_bp, @heart_rate, @notes, @reffered, @disease, @sub_diseases, @registered_date, @updated_date, @prescribed_medicines)"
cmd.CommandText = sSQL
Else
sSQL = "UPDATE patients-2015 SET first_name = @first_name, last_name = @last_name, gender = @gender, phone_number = @phone_number, age = @age, address = @address , height = @height, standing_bp = @standing_bp, heart_rate = @heart_rate, notes = @notes, reffered = @reffered, disease = @disease, sub_diseases = @sub_diseases, registered_date = @registered_date, standing_bp = @standing_bp, prescribed_medicines = @prescribed_medicines WHERE id = @id"
cmd.CommandText = sSQL
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.UInt64, .ParameterName = "@id"}).Value = Me.txtFirstName.Tag
End If
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@first_name"}).Value = IIf(Len(Trim(Me.txtFirstName.Text)) > 0, Me.txtFirstName.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@last_name"}).Value = IIf(Len(Trim(Me.txtLastName.Text)) > 0, Me.txtLastName.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@gender"}).Value = IIf(Not Me.ComboGender.Text = "Select", Me.ComboGender.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@phone_number"}).Value = IIf(Len(Trim(Me.TextPhoneNumber.Text)) > 0, Me.TextPhoneNumber.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@age"}).Value = IIf(Len(Trim(Me.txtAge.Text)) > 0, Me.txtAge.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@address"}).Value = IIf(Len(Trim(Me.txtAddress.Text)) > 0, Me.txtAddress.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@height"}).Value = IIf(Len(Trim(Me.txtHeight.Text)) > 0, Me.txtHeight.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@standing_bp"}).Value = IIf(Len(Trim(Me.txtBp.Text)) > 0, Me.txtBp.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@heart_rate"}).Value = IIf(Len(Trim(Me.txtHeartRate.Text)) > 0, Me.txtHeartRate.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@notes"}).Value = IIf(Len(Trim(Me.txtNotes.Text)) > 0, Me.txtNotes.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@reffered"}).Value = IIf(Len(Trim(Me.txtRefferer.Text)) > 0, Me.txtRefferer.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@disease"}).Value = IIf(Len(Trim(Me.ComboDisease.Text)) > 0, Me.ComboDisease.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@sub_diseases"}).Value = IIf(Len(Trim(Me.CheckedList_SubTopic.SelectedItems.ToString)) > 0, Me.CheckedList_SubTopic.SelectedItems.ToString, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.DateTime, .ParameterName = "@registered_date"}).Value = IIf(Len(Trim(DateAndTime.Now.ToLongTimeString)) > 0, DateAndTime.Now.ToLongTimeString, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.DateTime, .ParameterName = "@updated_date"}).Value = IIf(Len(Trim(DateAndTime.Now)) > 0, DateAndTime.Now, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "@prescribed_medicines"}).Value = IIf(Len(Trim(Me.txtPrescribedMedicines.Text)) > 0, Me.txtPrescribedMedicines.Text, DBNull.Value)
' Dim Result As Integer = cmd.ExecuteNonQuery
Dim Result As Integer = cmd.ExecuteNonQuery
'If the record is new then we have to get its ID so that we can edit it rightaway after the insertion.
If Me.txtFirstName.Tag = 0 Then
cmd.CommandText = "Select @@Identity"
'Set textbox tag property with the ID of new record
Me.txtFirstName.Tag = cmd.ExecuteScalar()
End If
If Result <> 1 Then
MessageBox.Show("Insert failed.")
Else
MessageBox.Show("Data has been saved.")
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
con.Close()
End Try
End Sub
答案 0 :(得分:0)
使用[patients-2015]
更新代码行,其中包含您编写的表名。例如下面,
sSQL = "INSERT INTO [patients-2015] ( first_name, last_name, gender, phone_number, age, address, height, standing_bp, heart_rate, notes, reffered, disease, sub_diseases, registered_date, updated_date, prescribed_medicines)"
因此,您需要在代码中的任何位置应用此更改,而不是[ ]
。
希望这能解决您的错误。