Example #1
0
        public int Insert(ICollection list)
        {
            MSSQLField[] writeable = GetFields(FieldFlags.Write);
            MSSQLField[] identity  = GetFields(FieldFlags.Auto);
            //REMOVE FIELD INDENTITY KEY
            if (identity.Length > 0)
            {
                var temp = writeable.ToList();
                temp.Remove(identity[0]);
                writeable = temp.ToArray();
            }
            string[] names  = new string[writeable.Length];
            string[] places = new string[writeable.Length];

            for (int i = 0; i < writeable.Length; ++i)
            {
                names[i]  = database.QuoteName(writeable[i].Name);
                places[i] = "{" + i + "}";
            }
            string fieldstr = string.Join(",", names);
            string valuestr = string.Join(",", places);
            string sql      = "INSERT INTO " + QuotedName +
                              "(" + fieldstr + ") VALUES(" + valuestr + ");";


            SqlParameter pID      = null;
            int          rowcount = 0;

            object[] parameters = new object[writeable.Length];
            using (MSSQLStatement stmt = database.Prepare(sql) as MSSQLStatement)
            {
                stmt.Prepare(writeable.Length);
                if (pID != null)
                {
                    stmt.Command.Parameters.Add(pID);
                }
                foreach (object obj in list)
                {
                    for (int i = 0; i < writeable.Length; ++i)
                    {
                        parameters[i] = writeable[i].GetValue(obj);
                        if (parameters[i] == null && writeable[i].DataType == MSSQLStatement.ByteArrayType)
                        {
                            parameters[i] = MSSQLStatement.BinaryNull;
                        }
                    }
                    rowcount += stmt.ExecNonQuery(parameters);
                    if (pID != null)
                    {
                        object idValue = Convert.IsDBNull(pID.Value) ? null : pID.Value;
                        identity[0].SetValue(obj, idValue);
                    }
                }
            }
            return(rowcount);
        }
Example #2
0
 public IQuery Eq(string name, object value)
 {
     if (value == null)
     {
         where.Append(database.QuoteName(name)).Append(" IS NULL");
     }
     else
     {
         where.Append(database.QuoteName(name))
         .Append(@"={").Append(N++).Append("}");
         values.Add(value);
     }
     return(this);
 }