Example #1
0
        public void Update(IDataSet dataSet)
        {
            _sberrors = new StringBuilder();

            //Hold temporary xml in memory.
            MemoryStream mem = new MemoryStream();

            //Validate only changes to the dataset
            dataSet.GetChanges().WriteXml(mem, XmlWriteMode.IgnoreSchema);
            //Reset current position in the temporary memory representation.
            mem.Position = 0;
            //Read schema.
            XmlSchema sch;

            //We don't need the full path because the FileStream class can reads relative paths.
            using (FileStream fs = new FileStream(dataSet.SchemaFile, FileMode.Open))
            {
                sch = XmlSchema.Read(fs, new ValidationEventHandler(this.OnValidation));
                sch.Compile(new ValidationEventHandler(this.OnValidation));
            }

            //Validate incoming data.
            XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(mem));

            reader.Schemas.Add(sch);
            reader.ValidationType          = ValidationType.Schema;
            reader.ValidationEventHandler += new ValidationEventHandler(this.OnValidation);

            //Read to end and check errors afterwards.
            while (reader.Read())
            {
            }

            if (_sberrors.Length != 0)
            {
                throw new ArgumentException(_sberrors.ToString(), "BaseDataSet");
            }

            SqlXmlCommand cmd = new SqlXmlCommand(_connection);

            cmd.CommandType = SqlXmlCommandType.DiffGram;
            cmd.SchemaPath  = dataSet.SchemaFile;
            SqlXmlAdapter ad = new SqlXmlAdapter(cmd);

            //Update database with DataSet data.
            ad.Update(dataSet.GetState());
        }