Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As New Staff
Dim strConn As String
strConn = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
Dim conn As New SqlConnection(strConn)
Dim strSql As String
strSql = "SELECT StaffID FROM Staff"
Dim cmd As New SqlCommand(strSql, conn)
Dim daMember As New SqlDataAdapter(cmd)
Dim ds As New DataSet
conn.Open()
daMember.Fill(ds, "Staff")
Dim i As Integer = ds.Tables("Staff").Rows.Count - 1
For Each dr As DataRow In ds.Tables("Staff").Rows
strSql = "Update CIOPassword SET CIPassword=@CI, COPassword = @CO WHERE StaffID=@id"
Dim cmd2 As New SqlCommand(strSql, conn)
Dim output1 As String = ""
Dim output2 As String = ""
Dim random As New Random()
Dim val, val2 As Integer
For j As Integer = 0 To 9
val = random.[Next](1, 36)
val2 = random.[Next](1, 36)
output1 += ChrW(IIf(val <= 26, 64 + val, (val - 27) + 48))
output2 += ChrW(IIf(val2 <= 26, 64 + val2, (val2 - 27) + 48))
Next
cmd2.Parameters.AddWithValue("@CI", output1)
cmd2.Parameters.AddWithValue("@CO", output2)
cmd2.Parameters.AddWithValue("@id", dr(0))
cmd2.ExecuteNonQuery()
Next
GridView1.DataBind()
conn.Close()
End Sub
基本上我每次单击按钮时都尝试用 2 个随机数更新每条记录,我现在的问题是,系统会更新记录但数据错误。例如,所有数据都应该是不同的(随机字符串),但是对于某些行,它使用相同的数据进行了更新,但是随机地,row1 row2 row3 的第 1 列和第 2 列具有完全相同的数据,然后第 3 行具有不同的数据,第二次, row1 row2 相同数据 row3 ro4 不同数据。它是随机顺序的。当我添加一个 MsgBox 在 For 循环中进行测试时,数据已正确更新为所有不同的数据。