在循环中加载 sql 参数时出错,VB.Net
Error loading sql parameters in a loop, VB.Net
我目前在 Microsoft visual studio express 2013 工作,后端为 sql。我正在尝试 运行 循环遍历 2 个组合框和一个 datetimepicker,用于选中复选框的任何实例。但是,我 运行 遇到了一个错误,它显示 "System.ArgumentException: No Mapping exists from Object type system.windows.forms.datetimepicker to a known managed provider native type." 当我 运行 代码时,我已经对参数值进行了观察,并且它没有将数据保存到变量之前sql 命令触发。我想我需要以不同的方式存储变量以允许访问变量。这是我的代码:
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'AuditID' FROM table1", conn1)
Dim reader1 As SqlDataReader = comm1.ExecuteReader
reader1.Read()
Dim AuditID As Integer = reader1("AuditID")
reader1.Dispose()
'Loop through all checkboxes and write into sql tables
Dim Run As Integer
For Run = 1 To 5
Dim LineItem = DirectCast(Me.Controls("CB" & Run), CheckBox)
If LineItem.Checked = True Then
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
'sql statements
'select ID
Using conn2 As New SqlConnection(connstring)
conn2.Open()
Using comm2 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'ID' FROM table1", conn1)
Dim reader As SqlDataReader = comm2.ExecuteReader
reader.Read()
Dim ID As Integer = reader("ID")
reader.Dispose()
'Insert into table audit line
Using conn3 As New SqlConnection(connstring)
conn3.Open()
Using comm3 As New SqlCommand("INSERT INTO table1 (ID, AuditID, DateStart, Freq, repeats) " _
& "VALUES (@ID, @AuditID, @DateStart, @Freq, @Repeats)", conn3)
With comm3.Parameters
.AddWithValue("@ID", ID)
.AddWithValue("@AuditID", AuditID)
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
End With
comm3.ExecuteNonQuery()
End Using
conn3.Close()
End Using
End Using
conn2.Close()
End Using
End If
Next
End Using
conn1.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
我的 try 语句在这一行停止了我的代码:
comm3.ExecuteNonQuery()
但是,我知道这个错误是在我添加参数时出现的,特别是这 3 行:
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
我正在尝试根据此处的设计名称使用循环语句获取这些变量:
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
程序似乎不喜欢使用上面的这些维度插入 sql table。有谁知道我可以将这些值传递给 sql 参数语句的方法吗?
我已经解决了这个问题,我需要将我的循环变量放入 .value 类型变量中。我添加了这个来修复它:
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
Dim Time As Date = DateTime.Value
Dim Freq As Integer = Frequency.SelectedValue
Dim Again As Integer = Repeat.SelectedValue
有很多事情我会做不同的事情。首先,始终使用 Option Strict
,它会捕获您正在进行的一些类型转换。
我会从显式列表中获取控件,而不是从 Controls
中获取控件。只需制作几个数组来保存控件引用,这样您就不需要在集合中找到它们:
Private DTPs As DateTimePicker() = {DTP1, DTP2...}
这将避免转换它们的需要,减少箍和隐式转换,如 "DTP" & Run
:
Dim dt As DateTime ' vars for the SQL
Dim freq As Integer
For Run As Integer = 0 To 4
dt = DTPs(Run).Value
freq = cboFreq(Run).SelectedValue
...
我目前在 Microsoft visual studio express 2013 工作,后端为 sql。我正在尝试 运行 循环遍历 2 个组合框和一个 datetimepicker,用于选中复选框的任何实例。但是,我 运行 遇到了一个错误,它显示 "System.ArgumentException: No Mapping exists from Object type system.windows.forms.datetimepicker to a known managed provider native type." 当我 运行 代码时,我已经对参数值进行了观察,并且它没有将数据保存到变量之前sql 命令触发。我想我需要以不同的方式存储变量以允许访问变量。这是我的代码:
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'AuditID' FROM table1", conn1)
Dim reader1 As SqlDataReader = comm1.ExecuteReader
reader1.Read()
Dim AuditID As Integer = reader1("AuditID")
reader1.Dispose()
'Loop through all checkboxes and write into sql tables
Dim Run As Integer
For Run = 1 To 5
Dim LineItem = DirectCast(Me.Controls("CB" & Run), CheckBox)
If LineItem.Checked = True Then
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
'sql statements
'select ID
Using conn2 As New SqlConnection(connstring)
conn2.Open()
Using comm2 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'ID' FROM table1", conn1)
Dim reader As SqlDataReader = comm2.ExecuteReader
reader.Read()
Dim ID As Integer = reader("ID")
reader.Dispose()
'Insert into table audit line
Using conn3 As New SqlConnection(connstring)
conn3.Open()
Using comm3 As New SqlCommand("INSERT INTO table1 (ID, AuditID, DateStart, Freq, repeats) " _
& "VALUES (@ID, @AuditID, @DateStart, @Freq, @Repeats)", conn3)
With comm3.Parameters
.AddWithValue("@ID", ID)
.AddWithValue("@AuditID", AuditID)
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
End With
comm3.ExecuteNonQuery()
End Using
conn3.Close()
End Using
End Using
conn2.Close()
End Using
End If
Next
End Using
conn1.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
我的 try 语句在这一行停止了我的代码:
comm3.ExecuteNonQuery()
但是,我知道这个错误是在我添加参数时出现的,特别是这 3 行:
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
我正在尝试根据此处的设计名称使用循环语句获取这些变量:
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
程序似乎不喜欢使用上面的这些维度插入 sql table。有谁知道我可以将这些值传递给 sql 参数语句的方法吗?
我已经解决了这个问题,我需要将我的循环变量放入 .value 类型变量中。我添加了这个来修复它:
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
Dim Time As Date = DateTime.Value
Dim Freq As Integer = Frequency.SelectedValue
Dim Again As Integer = Repeat.SelectedValue
有很多事情我会做不同的事情。首先,始终使用 Option Strict
,它会捕获您正在进行的一些类型转换。
我会从显式列表中获取控件,而不是从 Controls
中获取控件。只需制作几个数组来保存控件引用,这样您就不需要在集合中找到它们:
Private DTPs As DateTimePicker() = {DTP1, DTP2...}
这将避免转换它们的需要,减少箍和隐式转换,如 "DTP" & Run
:
Dim dt As DateTime ' vars for the SQL
Dim freq As Integer
For Run As Integer = 0 To 4
dt = DTPs(Run).Value
freq = cboFreq(Run).SelectedValue
...