// Add New Admin. public long Add(Administrator t) { long newId; using (SqlConnection conn = new SqlConnection(FlyingCenterConfig.CONNECTION_STRING)) { conn.Open(); using (SqlCommand cmd1 = new SqlCommand($"Select * from UserNames where User_Names = '{t.User_Name.ToUpper()}'", conn)) { SqlDataReader reader = cmd1.ExecuteReader(); if (reader.Read() == true || t.User_Name.ToUpper() == FlyingCenterConfig.ADMIN_NAME.ToUpper()) { throw new UserNameIsAlreadyExistException($"Sorry But '{t.User_Name}' Is Already Exist"); } conn.Close(); } using (SqlCommand cmd2 = new SqlCommand($"Insert Into Administrators(User_Name,Password) Values('{t.User_Name}','{t.Password}'); SELECT CAST(scope_identity() AS bigint)", conn)) { conn.Open(); cmd2.Parameters.AddWithValue($"{t.User_Name}", "bar"); newId = (long)cmd2.ExecuteScalar(); UserNames.AddUserName(t.User_Name); } } return(newId); }
// Add New Admin. public void Add(Administrator t) { using (SqlConnection conn = new SqlConnection(FlyingCenterConfig.CONNECTION_STRING)) { conn.Open(); using (SqlCommand cmd1 = new SqlCommand($"Select * from UserNames where User_Names = '{t.User_Name.ToUpper()}'", conn)) { SqlDataReader reader = cmd1.ExecuteReader(); if (reader.Read() == true || t.User_Name.ToUpper() == FlyingCenterConfig.ADMIN_NAME.ToUpper()) { throw new UserNameIsAlreadyExistException($"Sorry But '{t.User_Name}' Is Already Exist"); } conn.Close(); } using (SqlCommand cmd2 = new SqlCommand($"Insert Into Administrators(User_Name,Password) Values('{t.User_Name}','{t.Password}')", conn)) { conn.Open(); cmd2.ExecuteNonQuery(); UserNames.AddUserName(t.User_Name); } } }