ich diesen Code bin mit einzigartigem Code zu generierenwollen mehrere numarical Passwort generieren und an SQL Server senden C#
public static string CreateRandomPassword() //If you are always going to want 8 characters then there is no need to pass a length argument
{
string _allowedChars = "1234567899999";
Random randNum = new Random((int)DateTime.Now.Ticks); //Don't forget to seed your random, or else it won't really be random
char[] chars = new char[5];
//again, no need to pass this a variable if you always want 8
for (int i = 0; i < 5; i++)
{
chars[i] = _allowedChars[randNum.Next(_allowedChars.Length)];
}
return new string(chars);
Und mit dieser, es zu SQL Server senden
protected void Button1_Click(object sender, EventArgs e)
{
var sand = CreateRandomPassword();
int num;
if (int.TryParse(ids.Text,out num))
{
for (int i = 1; i <= num; i++)
{
string strcon = ConfigurationManager.ConnectionStrings["slxserv"].ToString();
using (SqlConnection con = new SqlConnection(strcon))
using (SqlCommand cmd = new SqlCommand("INSERT INTO passwords (password) VALUES (@password) "))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@password",sand);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Was ist das Problem? Warum musst du num mal dein Passwort eingeben? – Boo
Es generiert das Passwort, aber wenn Sie versuchen, mehrere einfügen, wird das gleiche Passwort in verschiedenen Zeile hinzugefügt. zum Beispiel, wenn ich 6 Passwort erstellen seine sechs Passwort erstellen, aber alle Passwörter sind gleich –
ja, einfügen bedeutet eine neue Zeile in sql einfügen, und Ihr Passwort wird einmal vor Ihren Schleifen generiert – Boo