想要生成多个数字密码并将其发送到 sql 服务器 c#

want to generate multiple numarical password and send it to sql server 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);

并使用它发送到 sql 服务器

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();
                }

替换

cmd.Parameters.AddWithValue("@password",沙);

cmd.Parameters.AddWithValue("@password",CreateRandomPassword());

首先将你的 Random randNum = new Random((int)DateTime.Now.Ticks); 放在方法 CreateRandomPassword() 之外并使其成为静态的

以及 Boo 说如何将这部分放入 for 循环 var sand = CreateRandomPassword();

因此最终代码将如下所示

static Random randNum = new Random((int)DateTime.Now.Ticks); 
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";

        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);
}

protected void Button1_Click(object sender, EventArgs e)
    {
        int num; 
        if ( int.TryParse(ids.Text,out num))
        {
            for (int i = 1; i <= num; i++)
            {
                var sand = CreateRandomPassword();
                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();
                }
}