我创建了下面的代码,在每行中给出相同的随机数。我的桌子上有10行20列。我可以在代码中添加什么,以便在每行中给出不同的结果?
Dim conStr As String
conStr = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
Try
Using con As New OleDb.OleDbConnection(conStr)
con.Open()
Dim rndN As New Random
Dim sqlStr As String = "UPDATE t_RandomData SET Column1 = @rndVal1, Column2 = @rndVal2, Column3 = @rndVal3, Column4 = @rndVal4, Column5 = @rndVal5, Column6 = @rndVal6, Column7 = @rndVal7, Column8 = @rndVal8, Column9 = @rndVal9, Column10 = @rndVal10, Column11 = @rndVal11, Column12 = @rndVal12, Column13 = @rndVal13, Column14 = @rndVal14, Column15 = @rndVal15, Column16 = @rndVal16, Column17 = @rndVal17, Column18 = @rndVal18, Column19 = @rndVal19, Column20 = @rndVal20"
Using cmd As New OleDb.OleDbCommand(sqlStr, con)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@rndVal1", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal2", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal3", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal4", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal5", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal6", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal7", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal8", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal9", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal10", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal11", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal12", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal13", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal14", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal15", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal16", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal17", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal18", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal19", rndN.Next(1, 99))
cmd.Parameters.AddWithValue("@rndVal20", rndN.Next(1, 99))
cmd.ExecuteNonQuery()
cmd.Dispose()
End Using
con.Close()
End Using
Catch ex As Exception
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "alert('" & ex.Message & "');", True)
End Try
答案 0 :(得分:2)
您使用的NULL_CHOICE = [...]
def get_tc_choices():
return NULL_CHOICE + list(models.TestCycle.objects.filter(
id__in=models.Feedback.objects.filter(done=False).values_list('test_cycle_id',flat=True)
))
[...]
class CleanUpSelectForm(forms.Form):
test_cycle = forms.ChoiceField(choices=get_tc_choices, required=False)
语句没有UPDATE
,更新了所有行。
WHERE
如果你想更新所有行,但每行都有不同的随机值,你可以使用这种基于sql的方法:
Dim sqlStr As String = "UPDATE t_RandomData SET Column1 = @rndVal1, Column2 = @rndVal2, Column3 = @rndVal3, Column4 = @rndVal4, Column5 = @rndVal5, Column6 = @rndVal6, Column7 = @rndVal7, Column8 = @rndVal8, Column9 = @rndVal9, Column10 = @rndVal10, Column11 = @rndVal11, Column12 = @rndVal12, Column13 = @rndVal13, Column14 = @rndVal14, Column15 = @rndVal15, Column16 = @rndVal16, Column17 = @rndVal17, Column18 = @rndVal18, Column19 = @rndVal19, Column20 = @rndVal20 WHERE KeyColumn = @ID"
如果您需要1-98:
,则会生成0到98之间的随机数SELECT ABS(CHECKSUM(NewId())) % 99
如果您需要为可以使用的每一行保留此值:
SELECT ABS(CHECKSUM(NewId())) % 98 + 1
更新:最后一种方法假定为sql-server。