示例#1
0
文件: Model.cs 项目: qrakhen/struqt
        /// <summary>
        /// Inserts this object's data into the table as a new row.
        /// </summary>
        protected virtual void insert()
        {
            var q = new Query.Insert(__tbl);

            q.output(__pkyc);
            foreach (var field in __def.fields.Values)
            {
                if (field.primary)
                {
                    continue;
                }
                q.addValue(field.column, RowWriter.write(field.type, this[field.name]));
            }
            var r = __db.scalar(q);

            this[__pkyn] = r;
        }
示例#2
0
        /// <summary>
        /// Executes provided query and returns the output column which is defined using Query.Insert.output();
        /// This is commonly used for previously undefined IDs, to retrieve the new ID directly, without having to use another call.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public object scalar(Query.Insert query)
        {
            object result = null;

            using (var sql = connect()) {
                using (var cmd = new SqlCommand(useDatabase(query.build()), sql)) {
                    foreach (var a in query.arguments)
                    {
                        if (a.Value != null)
                        {
                            cmd.Parameters.AddWithValue(a.Key, a.Value);
                        }
                    }
                    sql.Open();
                    result = cmd.ExecuteScalar();
                }
            }
            return(result);
        }