예제 #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);
        }
예제 #2
0
        public int Delete()
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("DELETE FROM ").Append(table.QuotedName);
            if (where.Length > 0)
            {
                sql.Append(" WHERE ").Append(where.ToString());
            }
            int rowcount = 0;

            using (MSSQLStatement stmt = database.Prepare(sql.ToString()) as MSSQLStatement)
                rowcount = stmt.ExecNonQuery(values.ToArray());
            return(rowcount);
        }