当我尝试使用表单插入我的数据库时,用户从下拉列表中选择,并使用日期选择器。
我得到的错误,是代码说它不能插入双倍,但是我没有双倍的任何地方!
任何想法???
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class Form3
Dim Con As SqlConnection
Dim cmd As New OleDbCommand
Dim sqlstring As String
Dim connstring As String
Dim ds As DataSet
Dim da As SqlDataAdapter
Dim mon As Boolean
Dim tues As Boolean
Dim wed As Boolean
Dim thur As Boolean
Dim fri As Boolean
Dim sat As Boolean
Dim sun As Boolean
Dim user As Integer
Dim chosen As Date
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30"
Con = New SqlConnection(connstring)
Con.Open()
Dim strSQL As String = "SELECT * from Users"
Dim da As New SqlDataAdapter(strSQL, Con)
Dim ds As New DataSet
da.Fill(ds, "Users")
With cboname
.DataSource = ds.Tables("Users")
.DisplayMember = "Name"
.ValueMember = "Id"
.SelectedIndex = 0
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
Dim Con As SqlConnection
Dim cmd As New OleDbCommand
Dim sqlstring As String
Dim connstring As String
Dim ds As DataSet
Dim da As SqlDataAdapter
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30"
Con = New SqlConnection(connstring)
Con.Open()
sqlstring = "Insert into Rota ('" + chosen + "','" + mon + "','" + tues + "','" + wed + "','" + thur + "','" + fri + "','" + sat + "','" + sun + "','" + user + "')"
da = New SqlDataAdapter(sqlstring, Con)
ds = New DataSet
da.Fill(ds, "Rota")
End Sub
Private Sub cbomon_CheckedChanged(sender As Object, e As EventArgs) Handles cbomon.CheckedChanged
mon = True
End Sub
Private Sub cbotues_CheckedChanged(sender As Object, e As EventArgs) Handles cbotues.CheckedChanged
tues = True
End Sub
Private Sub cbowed_CheckedChanged(sender As Object, e As EventArgs) Handles cbowed.CheckedChanged
wed = True
End Sub
Private Sub cbothurs_CheckedChanged(sender As Object, e As EventArgs) Handles cbothurs.CheckedChanged
thur = True
End Sub
Private Sub cbofri_CheckedChanged(sender As Object, e As EventArgs) Handles cbofri.CheckedChanged
fri = True
End Sub
Private Sub cbosat_CheckedChanged(sender As Object, e As EventArgs) Handles cbosat.CheckedChanged
sat = True
End Sub
Private Sub cbosun_CheckedChanged(sender As Object, e As EventArgs) Handles cbosun.CheckedChanged
sun = True
End Sub
Private Sub cboname_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboname.SelectedIndexChanged
user = cboname.SelectedIndex
End Sub
Private Sub dtp1_ValueChanged(sender As Object, e As EventArgs) Handles dtp1.ValueChanged
chosen = dtp1.Value
chosen.ToString()
MsgBox(chosen)
End Sub
End Class
答案 0 :(得分:3)
您正在使用+
运算符来连接字符串,DateTime和几个布尔值。看起来编译器允许chosen
使用它,但当它到达mon
时,它决定尝试将双方转换为Double来添加它们。
根据+运算符的documentation,如果操作的一侧是字符串,另一侧是数值,编译器将尝试将两者都转换为Double并添加它们。这就是你看到这个问题的原因。
作为第一次尝试,请考虑使用字符串连接运算符&
。
sqlstring = "Insert into Rota ('" & chosen & "','" & mon & "','" & tues & "','" & wed & "','" & thur & "','" & fri & "','" & sat & "','" & sun & "','" & user & "')"
转向Option Strict On将有助于在将来捕获这些错误。
正如其他人所说,你不应该以这种方式构建SQL字符串,而应该使用参数化查询。如果您正在使用OleDbCommand,它看起来就像这样(在我的头顶):
Using cmd = New OleDbCommand("Insert into Rota(?, ?, ?, ?, ?, ?, ?, ?, ?)")
With cmd.Parameters
.Add("@chosen", chosen)
.Add("@mon", mon)
.Add("@tues", tues)
.Add("@wed", wed)
.Add("@thur", thur)
.Add("@fri", fri)
.Add("@sat", sat)
.Add("@sun", sun)
.Add("@user", user)
End With
cmd.ExecuteNonQuery()
End Using