public void Delete <T>(string where = null, SqlTransaction transaction = null) where T : IModelBase { if (transaction != null) { var deleteQuery = ModelBaseHelper.CreateDeleteQuery <T>(where); SqlCommand cmd = new SqlCommand() { Connection = transaction.Connection, CommandText = deleteQuery, Transaction = transaction }; cmd.ExecuteNonQuery(); } else { using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); var deleteQuery = ModelBaseHelper.CreateDeleteQuery <T>(where); SqlCommand cmd = new SqlCommand() { Connection = conn, CommandText = deleteQuery }; cmd.ExecuteNonQuery(); conn.Close(); } } }
//public void InsertInto<T>(T item, bool clearTable = false) where T : IModelBase //{ // InsertInto<T>(new[] { item }, clearTable); //} public IEnumerable <long> InsertInto <T>(IEnumerable <T> dataList, bool clearTable = false) where T : IModelBase { var result = new List <long>(); using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); if (clearTable) { SqlCommand cmd = new SqlCommand() { Connection = conn, CommandText = ModelBaseHelper.CreateDeleteQuery <T>() }; var scalar = cmd.ExecuteScalar(); if (scalar != null) { result.Add((long)scalar); } } var insertQuery = ModelBaseHelper.CreateInsertQuery <T>(); foreach (var combiRow in dataList) { SqlCommand cmd = new SqlCommand() { Connection = conn, CommandText = insertQuery }; combiRow.AddAsParametersToSqlCommand(cmd); var scalar = cmd.ExecuteScalar(); if (scalar != null) { result.Add((long)scalar); } } conn.Close(); } return(result); }