I am trying to populate a column in a database with unique strings. How do I achieve this?
My current code just calls the same instance of the GetUniqueKey()
method so it just generates the same key. This method will only be run once. Like I said, I am trying to generate a unique key for each row. The GetUniqueKey()
method generates an alphanumeric string. How do I calla new instance of Hash.GetUniqueKey()
in order to get a new unique key?
private readonly int KEY_SIZE = 5;
public void Fill() {
connectionStringBuilder.InitialCatalog = "parts";
connection.ConnectionString = CONSTANTS.connStringParts;
string query = @"UPDATE parts SET [InternalID] = '" + Hash.GetUniqueKey(KEY_SIZE) + "'";
SqlCommand command = new SqlCommand(query, connection);
using(connection) {
try {
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
catch(Exception ex) {
MessageBox.Show(Resource1.DatabaseConnectionError, Resource1.Error, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
Did you consider to use Guid?
https://docs.microsoft.com/tr-tr/dotnet/api/system.guid.newguid?view=netframework-4.8
// Create and display the value of two GUIDs.
Guid g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());
// This code example produces a result similar to the following:
// 0f8fad5b-d9cb-469f-a165-70867728950e
// 7c9e6679-7425-40de-944b-e07fc1f90ae7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.