/// <summary> /// Inserts the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns>System.Int32.</returns> /// <exception cref="ArgumentNullException">item</exception> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="Exception"></exception> public int Insert(T item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } List <SqlParameter> sqlParameters; int id = 0; string sqlCommand; try { sqlParameters = _formatter.GetSqlParameters(item).ToList(); sqlCommand = _formatter.FormInsertSqlCommand(item); sqlCommand = string.Concat(sqlCommand, "SELECT SCOPE_IDENTITY()"); } catch (InvalidOperationException ex) { throw new InvalidOperationException(ex.Message); } try { // Create a connection using (SqlConnection connection = new SqlConnection(_connection.ConnectionString)) { connection.Open(); using (SqlCommand cmd = new SqlCommand(sqlCommand, connection)) { sqlParameters.ForEach(sqlParameter => cmd.Parameters.Add(sqlParameter)); //BIGINT in sql id = (int)(decimal)cmd.ExecuteScalar(); item.Id = id; } } } catch (Exception ex) { throw new Exception(ex.Message); } return(id); }
/// <summary> /// Inserts the specified item. /// </summary> /// <param name="item">The item.</param> /// <exception cref="ArgumentNullException">item</exception> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="Exception"></exception> public void Insert(T item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } List <OleDbParameter> sqlParameters; string sqlCommand; try { sqlParameters = _formatter.GetOleDbParameters(item).ToList(); sqlCommand = _formatter.FormInsertSqlCommand(item); } catch (InvalidOperationException ex) { throw new InvalidOperationException(ex.Message); } try { using (OleDbConnection connection = new OleDbConnection(_connection.ConnectionString)) { connection.Open(); using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection)) { sqlParameters.ForEach(sqlParameter => cmd.Parameters.Add(sqlParameter)); cmd.ExecuteNonQuery(); } } } catch (Exception ex) { throw new Exception(ex.Message); } }