本文介绍了没有任何重复的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想在读取数据库的(min,max)个值之间创建一个随机数,然后选择一个保存在数据库中的数字.
我的程序是抽奖,没有重复号码.有人可以帮助我吗?

hi
i want create a random number between (min,max)values that read of my database and select a this number that saved in database .
my program is for draw and i do not have repeat number . anybody can help me??

推荐答案



 public bool VerifyInDB(string/int RandomVariable)
{
bool b_RandomStatus=false;
sqlconnection con_SQLConnectionString=new sqlConnection(Constr);
SqlDataAdapter da_ForCheckforRandomInDB=new  SqlDataAdapter("select * from DB..tbl_table where RandomColumnName="+ randomvariable, con_SQLConnectionString);

DataTable dt_ForCheckforRandomInDB=new DataTable()
da.fill(dt_ForCheckforRandomInDB)
if(dt_ForCheckforRandomInDB.Rows.Count()>0)
	{
		b_RandomStatus=true;

	}
return b_RandomStatus;

}



步骤2.在相同的* .cs文件中创建另一个方法,如下所示



Step 2. Create another method in the same *.cs file as follows

 public string/int CreateRandomNumber()
{
        //Assign random value here
	string/int RandomVariable ;//=Random();
	if(VerifyInDB(RandomVariable))
		{
                  //Random value Present in DB Call CreateRandomNumber() again
			CreateRandomNumber()
		}

Return RandomVariable;

}


步骤3:在代码中的任何位置调用CreateRandomNumber(),这将返回数据库中不存在的随机值.

步骤4:int/String RandumValue = CreateRandomNumber();您将获得在数据库中不会重复的随机值.


Step 3: Call CreateRandomNumber() any where in your code this will return random value which is not present in your DataBase.

Step 4:int/String RandumValue=CreateRandomNumber(); you will get the random value which is not repeated in DataBase.


这篇关于没有任何重复的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:25