Example #1
0
        public static InsertStatement CreateInsertStatement(IMapping mapping, bool identityInsert = false)
        {
            InsertStatement statement = new InsertStatement(mapping.TableName);

            statement.IdentityColumnName = String.Empty;

            foreach (var item in mapping.Columns)
            {
                if (item.IsDbGenerated && !identityInsert)
                {
                    statement.IdentityColumnName = item.Name;
                }
                else
                {
                    var          pinfo = (PropertyInfo)item.Property;
                    SqlParameter prm;
                    if (pinfo.PropertyType.IsGenericType && pinfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        prm = statement.CreateParameter(item.Name, pinfo.PropertyType.GetGenericArguments()[0]);
                    }
                    else
                    {
                        prm = statement.CreateParameter(item.Name, pinfo.PropertyType);
                    }

                    if (item.IsNullable && item.DefaultValue != null && prm.Value == null)
                    {
                        prm.Value = item.DefaultValue;
                    }

                    statement.Column(item.ColumnName, prm);
                }
            }

            return(statement);
        }
Example #2
0
        protected int InsertRows(string table_name)
        {
            DataTable         myTable = this.Tables[table_name];
            DataRowCollection rows    = myTable.Rows;
            int rowsCount             = rows.Count;
            int rowsAffected          = 0;

            if (rowsCount <= 0)
            {
                return(rowsAffected);
            }


            var cmd = new InsertStatement(myTable.ExtendedProperties["Storage"].ToString());

            cmd.ConnectionStringName = _connectionStringName;
            var prmlist   = new List <SqlParameter>();
            var colsCount = myTable.Columns.Count;

            var identcol = String.Empty;

            for (var c = 0; c < colsCount; c++)
            {
                var col = myTable.Columns[c];
                var prm = new SqlParameter(col.ColumnName, col.DataType);
                prmlist.Add(prm);
                if (col.AutoIncrement)
                {
                    identcol = col.ColumnName;
                }
                else if (col.ExtendedProperties["NonStorable"] == null)
                {
                    cmd.Column(col.ExtendedProperties["Storage"].ToString(), prm);
                }
            }

            IEnumerator rowEnum = rows.GetEnumerator();

            while (rowEnum.MoveNext())
            {
                var row = (DataRow)rowEnum.Current;
                if (row.RowState != DataRowState.Added)
                {
                    continue;
                }
                for (var c = 0; c < colsCount; c++)
                {
                    var col = myTable.Columns[c];
                    var prm = prmlist[c];
                    if (row[prm.Name] == null || row[prm.Name] == DBNull.Value && col.DefaultValue != null)
                    {
                        prm.Value = col.DefaultValue;
                    }
                    else
                    {
                        prm.Value = row[prm.Name];
                    }
                }

                if (identcol != String.Empty)
                {
                    object id;
                    cmd.Execute(out id);
                    row[identcol] = id;//Int32.Parse(id.ToString());
                }
                else
                {
                    cmd.Execute();
                }
                row.AcceptChanges();
                rowsAffected++;
            }
            return(rowsAffected);
        }