من از این روش استفاده میکنم و مشکلی هم نداشتم تا حالا
string sqlCmd = "if not exists(Select Code from codelist where code = @code) insert into codelist(Code) Values(@Code)";
SqlConnection sqlConnection = new SqlConnection("Data Source=Client1;Initial Catalog=code;User ID=sa;Password=master");
SqlCommand sqlCommand = new SqlCommand(sqlCmd, sqlConnection);
sqlCommand.Parameters.Add("@Code",SqlDbType.Decima l);
Random clsRandom = new Random();
int index = 0;
sqlConnection.Open();
do
{
decimal d = (decimal)clsRandom.Next(10000000, 99999999);
sqlCommand.Parameters["@Code"].Value = d;
sqlCommand.ExecuteNonQuery();
index += 1;
} while (index < 40000);
sqlConnection.Close();