Professional Documents
Culture Documents
Doce Code Game Spring
Doce Code Game Spring
******************
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* The above copyright notice and this permission notice shall be included in all copies or
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************************************************************
****************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
namespace CodeGenerator
/// <summary>
/// </summary>
/// <example>
/// <code>
/// {
/// connection.Open();
/// var codeLength = GetCurrentCodeLength(connection);
/// {
/// Console.WriteLine(code);
/// {
/// SaveNewCodeLength(generator.CodeLength);
/// NotifyDeveloperOfApproachingCodePoolExhaustion(
/// generator.CodeLength,
/// CodeGenerator.MaxCodeLength);
/// }
/// }
/// }
/// </code>
/// </example>
SELECT Code
FROM dbo.Voucher v
SELECT Code
FROM dbo.Voucher
{
new SqlMetaData("Code", SqlDbType.NVarChar, MaxCodeLength)
};
/// <summary>
/// Create a CodeGenerator instance.
/// </summary>
/// The connection to the database. Must be open. Calling code is responsible for
/// creating, opening, and disposing the connection.
/// </param>
/// The initial code length, which will grow as needed as codes are used up. However, you
/// still need to persist the <c>CodeLength</c> property value and initialize this parameter
/// <paramref name="codeLength"/> - say "4", then ALL of the 4-digit codes will eventually
/// become used up instead of maintaining the sparseness dictated by the collision
/// threshold.
/// </param>
{}
/// <summary>
/// </summary>
/// The connection to the database. Must be open. Calling code is responsible for
/// The initial code length, which will grow as needed as codes are used up. However, you
/// still need to persist the <c>CodeLength</c> property value and initialize this parameter
/// <paramref name="codeLength"/> - say "4", then ALL of the 4-digit codes will eventually
/// </param>
/// The number of codes to generate, test, and insert at once. Tune this value for best
/// performance. In my tests, 500 worked well.
/// </param>
/// A value between 0 (inclusive) and 1 (exclusive). Supply a small value (perhaps 0.01) to
/// keep codes sparse. A value that is too high (above 0.5) will result in sub-optimum
/// performance.
/// </param>
_connection = connection;
CodeLength = codeLength;
_batchSize = batchSize;
_collisionThreshold = collisionThreshold;
_batchParameter.TypeName = "dbo.VoucherCodeList";
if (_disposed)
return;
_command.Dispose();
_disposed = true;
/// <summary>
/// Generates unique random codes and inserts them into the database.
/// </summary>
/// <param name="numberOfCodes">The number of codes you need.</param>
CodeLength++;
}
return result;
_builder.Clear();
_builder.Append(AvailableChars[_random.Next(AvailableChars.Length)]);
return _builder.ToString();
}
});
while (reader.Read())
/// <summary>
command.CommandText = @"
);";
command.ExecuteNonQuery();
}
}
}