public void Update(PeopleModel record) { _command.Parameters.Clear(); _command.CommandText = "use Phonebook; UPDATE People " + "SET FirstName = @FirstName, LastName = @LastName, " + "Phone = @Phone, Email = @Email, " + "Created = @Created, Updated = @Updated " + "WHERE Id=@Id;"; var paramList = new List <object>(); paramList.Add(new SqlParameter("@Id", SqlDbType.Int) { Value = record.Id }); paramList.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar) { Value = record.FirstName }); paramList.Add(new SqlParameter("@LastName", SqlDbType.NVarChar) { Value = record.LastName }); paramList.Add(new SqlParameter("@Phone", SqlDbType.NVarChar) { Value = record.Phone }); paramList.Add(new SqlParameter("@Email", SqlDbType.NVarChar) { Value = record.Email }); paramList.Add(new SqlParameter("@Created", SqlDbType.DateTime2) { Value = record.Created }); paramList.Add(new SqlParameter("@Updated", SqlDbType.DateTime2) { Value = DateTime.Now }); _command.Parameters.AddRange(paramList.ToArray()); Open(); _command.ExecuteNonQuery(); Close(); }
public int Add(PeopleModel record) { _command.Parameters.Clear(); _command.CommandText = "use Phonebook; INSERT INTO People(FirstName,LastName,Phone,Email,Created,Updated) " + "VALUES (@FirstName,@LastName,@Phone,@Email,@Created,@Updated); select SCOPE_IDENTITY();"; var paramList = new List <object>(); var dt = DateTime.Now; paramList.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar) { Value = record.FirstName }); paramList.Add(new SqlParameter("@LastName", SqlDbType.NVarChar) { Value = record.LastName }); paramList.Add(new SqlParameter("@Phone", SqlDbType.NVarChar) { Value = record.Phone }); paramList.Add(new SqlParameter("@Email", SqlDbType.NVarChar) { Value = record.Email }); paramList.Add(new SqlParameter("@Created", SqlDbType.DateTime2) { Value = dt }); paramList.Add(new SqlParameter("@Updated", SqlDbType.DateTime2) { Value = dt }); _command.Parameters.AddRange(paramList.ToArray()); Open(); var newId = Convert.ToInt16(_command.ExecuteScalar()); Close(); return(newId); }