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 [email protected], COPassword = @CO WHERE [email protected]"
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相同的數據row3。它是隨機的。當我添加一個MsgBox在For循環中進行測試時,所有不同的數據都正確更新了數據。隨機對象多次返回相同的結果
非常感謝,您的評論是有用的我解決了我的問題。謝謝 – user236501