我是Vb.net的新手。
我试图在SQL server
中一个一个地插入一个List(Of Object),但我无法弄清楚如何正确地进行操作。
我将这个列表的副本复制到了DataDrigView
(dgv_Test)中。为了使您了解内容,列表包含每个都有一个元素列表的对象,依此类推,在插入之后,我将更新“时间”列以设置插入所花费的时间。我在第二个嵌套循环中收到错误代码
System.Data.SqlClient.SqlException:'无效的列名;
错误显示我尝试插入的特定行数据
这是我的代码;
我有一个功能; GetStringSql_InsertBD_Object_Type
将对象带入参数并检查GetType.Name
以获取正确的字符串以进行插入;这是查询的示例;
sSQL_test = "INSERT INTO [Delivery] (prov_orgn,city_orgn,prov_dest,city_dest)"
sSQL_test = sSQL_test + " OUTPUT INSERTED.id"
sSQL_test = sSQL_test + " VALUES ( " & deliv.prov_orgn & "," & deliv.city_orgn & "," & deliv.prov_dest & "," & livraisonTest.city_dest & ")"
该函数返回sSQL_test
Dim connectionStringTest As String = _conn_test
Dim conn_test As SqlConnection = New SqlConnection(connectionStringTest)
Static start_time As DateTime
Static stop_time As DateTime
Dim timer As TimeSpan
Dim time As String
For Each test_auto As Test_Auto In lst_commandes_test
start_time = Now
Dim cmdString As String = GetStringSql_InsertBD_Object_Type(test_auto)
Dim sqlComm1 As New SqlCommand(cmdString, conn_test)
conn_test.Open()
Dim reader1 As SqlDataReader = sqlComm1.ExecuteReader()
If reader1.HasRows Then
Dim stri As String = CStr(reader1.Read())
test_auto.No_Test = pos
id_return_output = stri
test_auto.id = id_return_output
reader1.Close()
End If
conn_test.Close()
For Each lvr_test As Livr_Test In test_auto.lst_Liv
'lvr_test = ii
string_insert = GetStringSql_InsertBD_Object_Type(lvr_test)
Dim sqlComm2 As New SqlCommand(string_insert, conn_test)
conn_test.Open()
'error happens line below;
Dim reader2 As SqlDataReader = sqlComm2.ExecuteReader()
If (reader2.HasRows) Then
Dim stri2 As String = CStr(reader2.Read())
lvr_test.id_liv = stri2
End If
conn_test.Close()
For Each item_lvr_test As Item_Livr_Test In lvr_test.lst_Item
'item_lvr_test = lst_commandes_test(item).lst_Liv(lvr_test).lst_Item(iii)
string_insert = GetStringSql_InsertBD_Object_Type(item_lvr_test)
Dim sqlComm3 As New SqlCommand(string_insert, conn_test)
conn_test.Open()
Dim reader3 As SqlDataReader = sqlComm3.ExecuteReader()
If (reader3.HasRows) Then
Dim stri3 As String = CStr(reader3.Read())
item_lvr_test.id_item = stri3
End If
conn_test.Close()
Next
Next
stop_time = Now
timer = stop_time.Subtract(start_time)
time= CStr(timer .TotalSeconds.ToString("0.000000"))
dgvTest.Rows(pos).Cells("time").Value = temps
pos += 1
Next