public void AddSimpleAd(SimpleAd ad) { using var connection = new SqlConnection(_connectionString); using var command = connection.CreateCommand(); command.CommandText = "INSERT INTO Ads (Description, PhoneNumber, DateCreated, UserId) " + "VALUES (@desc, @phone, GETDATE(), @userId) SELECT SCOPE_IDENTITY()"; command.Parameters.AddWithValue("@desc", ad.Description); command.Parameters.AddWithValue("@phone", ad.PhoneNumber); command.Parameters.AddWithValue("@userId", ad.UserId); connection.Open(); ad.Id = (int)(decimal)command.ExecuteScalar(); }
private SimpleAd GetAdFromReader(SqlDataReader reader) { var ad = new SimpleAd { Description = reader.Get <string>("Description"), Date = reader.Get <DateTime>("DateCreated"), PhoneNumber = reader.Get <string>("PhoneNumber"), Id = reader.Get <int>("Id"), UserId = reader.Get <int>("UserId"), PosterName = reader.Get <string>("Name") }; return(ad); }
public void AddSimpleAd(SimpleAd ad) { using var ctx = new SimpleAdsContext(_connectionString); ctx.Ads.Add(ad); ctx.SaveChanges(); }