Esempio n. 1
0
        private static string GetUpdateString(IDataEntity entity, string columns)
        {
            var separator   = new[] { ", " };
            var columnNames = columns.Split(separator, StringSplitOptions.None);
            var values      = entity.ToSqlString().Split(separator, StringSplitOptions.None);

            var sb     = new StringBuilder("set ");
            var length = columnNames.Length;

            for (int i = 0; i < length; i++)
            {
                sb.Append($"{columnNames[i]} = {values[i]}, ");
            }
            sb.Remove(sb.Length - 2, 2);
            return(sb.ToString());
        }
Esempio n. 2
0
        private protected void AddEntity(IDataEntity entity, string table, string columns)
        {
            var connection = new SqlConnection(ConnectionString);
            var com        = $"insert into {table}({columns}) values ({entity.ToSqlString()})";

            using (connection)
            {
                var command = new SqlCommand(com, connection);
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (SqlException e)
                {
                    throw new DbAccessException(e.Message, e);
                }
            }
        }