要求:
1。批量,也就是说一次一个循环可能要生成大量数字串(万为单位)。
2。16位或12位或14位数字串.
3。批量生成的这些串不能有重复串。高手指教

解决方案 »

  1.   

    http://www.cnblogs.com/lovecherry/archive/2005/03/25/125529.html
      

  2.   

    public class UBISerials
    {
    public static void MakeSerials(int num,MyRandom rnd,string batchid)
    {
    //递归调用
    int ierr=InsertSerials(num,rnd,batchid);
    if(ierr>0)
    MakeSerials(ierr,rnd,batchid);
    else
    {
    SqlConnection conn=new SqlConnection(Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["conn"]));
    SqlCommand cmd=new SqlCommand("sp_SerialBatchState",conn);
    //CREATE PROCEDURE sp_batchstate
    //@batchid nvarchar(50)
    //as
    //update batch set batch_state=0 where batch_id=@batchid
    //GO
    cmd.CommandType=CommandType.StoredProcedure;
    SqlParameter parm1=new SqlParameter("@batchid",SqlDbType.VarChar,50);
    parm1.Value=batchid;
    cmd.Parameters.Add(parm1);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    } } public static int ResetNumber(string batchid)
    {
    SqlConnection conn=new SqlConnection(Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["conn"]));
    SqlCommand cmd=new SqlCommand("sp_SerialsNumberReset",conn);
    cmd.CommandType=CommandType.StoredProcedure;
    SqlParameter parm1=new SqlParameter("@batchid",SqlDbType.VarChar,50);
    parm1.Value=batchid;
    cmd.Parameters.Add(parm1);
    SqlParameter parm2=new SqlParameter("@notdonum",SqlDbType.Int);
    parm2.Direction=ParameterDirection.Output;
    cmd.Parameters.Add(parm2);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    return (int)cmd.Parameters[1].Value;
    } public static int InsertSerials(int num,MyRandom rnd,string batchid)
    {
    int ierr=0;
    SqlConnection conn=new SqlConnection(Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["conn"]));
    SqlCommand cmd=new SqlCommand("sp_SerialAdd",conn);
    //CREATE PROCEDURE sp_addkey
    //@key nvarchar(50),@batchid nvarchar(50)
    //as
    //insert into keys (keys_key,keys_batch_id)values(@key,@batchid)
    //GO
    cmd.CommandType=CommandType.StoredProcedure;
    SqlParameter parm1=new SqlParameter("@key",SqlDbType.VarChar,50);
    SqlParameter parm2=new SqlParameter("@batchid",SqlDbType.VarChar,50);
    parm2.Value=batchid;
    cmd.Parameters.Add(parm1);
    cmd.Parameters.Add(parm2);
    conn.Open();
    for(int i=0;i<num;i++)
    {
    try
    {
    parm1.Value=rnd.GetRandomNum();
    cmd.ExecuteNonQuery();
    }
    catch
    {
    ierr++;
    }
    }
    conn.Close();
    return ierr;
    }
    }中间那个ResetNumber别去看他
    数据库建立一个表把key作为主见,利用递归和异常来做到生成定数量的不重复序列号
      

  3.   

    Random r=new Random(System.Environment.TickCount);
    ArrayList al=new ArrayList();
    string sb;
    for(int i=0;i<10000;i++)
    {
    sb=DateTime.Now.ToString("yyyyMMdd").ToString()+System.Environment.TickCount.ToString();
    System.Threading.Thread.Sleep(15);
    al.Add(sb);
    }试试看。
      

  4.   

    写入数据库前,先在数据库里查询一下,有没有重复的随机串,有则重新生成
    >>我做过一下尝试
    假设数据库已经存在100万条记录
    不作索引进行查询和插入 50万条 5小时
    作索引进行查询和插入 50万条 1小时
    以上的效率都没有用主键进行捕获异常效率来的高
    所以还是选择用主键+异常捕获+递归调用=精确的生成一定数量的随机数
      

  5.   

    以上方法在我的应用中好像是不可行的。
    例如: LoveCherry的方法是可以生成批量无重复数字串,但要先对库里的数据检查有无相同的然后再存到库里,而我现在希望就是在保存到数据库中之前就保证所有生成的数字串无重复,然后我要对每一串进行加密再保存到库里。还有一点,现在偶要生成的数字串要确定在14位。
    不知道高手们明白否?
    大家各出良策吧,也许你的想法就能激活大家很多妙招的!
      

  6.   

    lovecherry的“用主键+异常捕获+递归调用=精确的生成一定数量的随机数”
    方法不知道在进行50万条记录的时候大概需要多久?
      

  7.   

    哦 不好意思 刚才没有仔细看lovecherry的方法。
    你的方法是生成随机数插入库中,捕获异常(某条插入失败),然后将失败的那些再重新生成随机数插入库中,如此循环达到插入n条记录的目的,不知理解的是否正确。
    这个方法应该是可行的。不知道效率到底怎么样。这里可能需要一次生成50万条记录,而速度希望在几分钟之内。
      

  8.   

    to lovecherry:你的MyRandom 随机数产生的方法有吗? 可以给我看看吗?
      

  9.   

    应该不会
    我们公司需要生成几百万条,我用windows服务做的,服务器cpu占用仅仅3%