Beispiel #1
0
        public int Insert(object obj)
        {
            System.Type type      = obj.GetType();
            DbCommand   command   = this.getCommand(type, new Func <System.Type, string>(DatabaseUtilities.GetInsertStatement));
            string      returnSql = command.CommandText;
            string      newId     = "InternalNewId";

            if (command is System.Data.SqlClient.SqlCommand)
            {
                returnSql = "; SET @InternalNewId = SCOPE_IDENTITY()";
                newId     = "@" + newId;
            }
            else
            {
                returnSql = string.Format("RETURNING {0} INTO :InternalNewId", DatabaseUtilities.GetKeyColumn(type).Key);
                newId     = ":" + newId;
            }
            DbCommand expr_7A = command;

            expr_7A.CommandText += returnSql;
            this.AddParametersFrom <object>(command, obj);
            this.AddOutParameter(command, newId, System.Data.DbType.Int32, 10);
            this.ExecuteNonQueryCommand(command);
            return((int)command.Parameters[newId].Value);
        }
        public static string GetInsertStatement(System.Type type)
        {
            Dictionary <string, string> columnNames = DatabaseUtilities.getColumns(type);

            columnNames.Remove(DatabaseUtilities.GetKeyColumn(type).Key);
            return(string.Format("INSERT INTO {0}({1}) VALUES({2})", DatabaseUtilities.GetTableName(type), string.Join(",", columnNames.Keys), string.Join(",", columnNames.Values)));
        }
        public static string GetUpdateStatement(System.Type type)
        {
            Dictionary <string, string> columnNames = DatabaseUtilities.getColumns(type);

            columnNames.Remove(DatabaseUtilities.GetKeyColumn(type).Key);
            return(string.Format("UPDATE {0} SET {1}", DatabaseUtilities.GetTableName(type), string.Join(",",
                                                                                                         from x in columnNames
                                                                                                         select string.Format("{0} = {1}", x.Key, x.Value))));
        }
Beispiel #4
0
        public T Get <T>(int id)
        {
            DbCommand command     = this.getCommand(typeof(T), new Func <System.Type, string>(DatabaseUtilities.GetSelectStatement));
            string    whereClause = string.Format(" WHERE {0} = {1}", DatabaseUtilities.GetKeyColumn(typeof(T)).Key, (command is System.Data.SqlClient.SqlCommand) ? "@id" : ":id");
            DbCommand expr_56     = command;

            expr_56.CommandText += whereClause;
            this.AddInParameter(command, (command is System.Data.SqlClient.SqlCommand) ? "@id" : ":id", System.Data.DbType.Int64, id);
            return(this.ExecuteAndReturn <T>(command).FirstOrDefault <T>());
        }
Beispiel #5
0
        public int Update(object obj)
        {
            System.Type type = obj.GetType();
            System.Collections.Generic.KeyValuePair <string, string> key = DatabaseUtilities.GetKeyColumn(type);

            DbCommand command     = this.getCommand(type, new Func <System.Type, string>(DatabaseUtilities.GetUpdateStatement));
            string    whereClause = string.Format(" WHERE {0} = {1}", key.Key, (command is System.Data.SqlClient.SqlCommand) ? ("@" + key.Value) : (":" + key.Value));
            DbCommand expr_6B     = command;

            expr_6B.CommandText += whereClause;
            this.AddParametersFrom <object>(command, obj);
            return(this.ExecuteNonQueryCommand(command));
        }
Beispiel #6
0
        public int Delete <T>(int id)
        {
            System.Type type    = typeof(T);
            DbCommand   command = this.GetSqlStringCommand("DELETE FROM " + DatabaseUtilities.GetTableName(type));

            System.Collections.Generic.KeyValuePair <string, string> key = DatabaseUtilities.GetKeyColumn(type);
            string    prefix      = (command is System.Data.SqlClient.SqlCommand) ? "@" : ":";
            string    whereClause = string.Format(" WHERE {0} = {1}{2}", key.Key, prefix, key.Value);
            DbCommand expr_5C     = command;

            expr_5C.CommandText += whereClause;
            this.AddInParameter(command, prefix + key.Value, System.Data.DbType.Int32, id);
            return(this.ExecuteNonQueryCommand(command));
        }