生成要作为参数添加到 sql 语句 vb.net 中的奇数
Generate odd number to be added as parameter into sql statement vb.net
我目前正在 vb.net windows 表单应用程序中工作,后端是 sql。我正在尝试使用 for 循环将奇数写入从 1 开始的 sql 列,并向上移动 datagridview 中的行数。但是,我的 For 语句没有给我下一步,并且根据我的 for 循环位置,我要么错过将我的行写入我的 sql 数据库的循环,要么我错过奇数。这是我的代码:
For cn As Integer = 0 To Datagridview1.RowCount - 1
Dim StartTime As Date = Datagridview1.Rows(cn).Cells(1).Value
For i As Integer = 1 To cn
'sql code
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("INSERT INTO table1 (col1, col2, col3, col4, col5) VALUES (@col1, @col2, getdate(), 5, @col5)", conn1)
With comm1.Parameters
.AddWithValue("@col1", StartTime)
.AddWithValue("@Col2", Combobox1.selectedvalue)
.AddWithValue("@Col5", i)
End With
comm1.ExecuteReader()
End Using
conn1.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next
Next
如果我很了解你的要求,你可以这样写
For cn As Integer = 1 To Datagridview1.RowCount
Dim StartTime As Date = Datagridview1.Rows(cn-1).Cells(1).Value
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("INSERT INTO table1 (col1, col2, col3, col4, col5) VALUES (@col1, @col2, getdate(), 5, @col5)", conn1)
With comm1.Parameters
.AddWithValue("@col1", StartTime)
.AddWithValue("@Col2", Combobox1.selectedvalue)
.AddWithValue("@Col5", (cn * 2) - 1)
End With
comm1.ExecuteReader()
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next
我目前正在 vb.net windows 表单应用程序中工作,后端是 sql。我正在尝试使用 for 循环将奇数写入从 1 开始的 sql 列,并向上移动 datagridview 中的行数。但是,我的 For 语句没有给我下一步,并且根据我的 for 循环位置,我要么错过将我的行写入我的 sql 数据库的循环,要么我错过奇数。这是我的代码:
For cn As Integer = 0 To Datagridview1.RowCount - 1
Dim StartTime As Date = Datagridview1.Rows(cn).Cells(1).Value
For i As Integer = 1 To cn
'sql code
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("INSERT INTO table1 (col1, col2, col3, col4, col5) VALUES (@col1, @col2, getdate(), 5, @col5)", conn1)
With comm1.Parameters
.AddWithValue("@col1", StartTime)
.AddWithValue("@Col2", Combobox1.selectedvalue)
.AddWithValue("@Col5", i)
End With
comm1.ExecuteReader()
End Using
conn1.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next
Next
如果我很了解你的要求,你可以这样写
For cn As Integer = 1 To Datagridview1.RowCount
Dim StartTime As Date = Datagridview1.Rows(cn-1).Cells(1).Value
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("INSERT INTO table1 (col1, col2, col3, col4, col5) VALUES (@col1, @col2, getdate(), 5, @col5)", conn1)
With comm1.Parameters
.AddWithValue("@col1", StartTime)
.AddWithValue("@Col2", Combobox1.selectedvalue)
.AddWithValue("@Col5", (cn * 2) - 1)
End With
comm1.ExecuteReader()
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next