Chodzi mi o te powtarzające się linijki w drugiej części funkcji. To jest poprawne rozwiązanie? A jeżeli nie to w jaki sposób fajnie utworzyć SqlCommand
z zapytaniem który wstawi informacje z obiektu do bazy?
public static SqlCommand InsertWorker(Model.Worker worker, SqlConnection connection)
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText =
@"INSERT INTO Metadata
(Name, Surname, Address, ContactPhone, ContactMail, Website, Description)
VALUES
(@FirstName, @LastName, @Address, @Phone, @Mail, @Website, Description);
INSERT INTO Workers
(Role, MetadataId, Username, Password)
VALUES
(@Privliges, CAST(scope_identity() AS int), @Username, @PasswordHash);";
command.Parameters.Add("@Privliges", SqlDbType.Int ).Value = worker.Privliges.Raw();
command.Parameters.Add("@Username", SqlDbType.NChar, 16).Value = worker.Username;
command.Parameters.Add("@PasswordHash", SqlDbType.NChar, 40).Value = worker.PasswordHash;
command.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = worker.Personal.FirstName;
command.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = worker.Personal.LastName;
command.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = worker.Personal.Address;
command.Parameters.Add("@Phone", SqlDbType.Char, 12).Value = worker.Personal.ContactPhone;
command.Parameters.Add("@Mail", SqlDbType.VarChar, 50).Value = worker.Personal.ContactMail;
command.Parameters.Add("@Website", SqlDbType.VarChar, 50).Value = worker.Personal.Website;
command.Parameters.Add("@Description", SqlDbType.VarChar, 50).Value = worker.Personal.Description;
return command;
}
PS: Zapytanie w sumie sam sklejałem więc proszę o wyrozumiałość.