Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Tables[0]。行) { 对于(int i=0;i_C#_Xsd_Dataset - Fatal编程技术网

C# .Tables[0]。行) { 对于(int i=0;i

C# .Tables[0]。行) { 对于(int i=0;i,c#,xsd,dataset,C#,Xsd,Dataset,因此,现在您可能需要有关如何保留数据记录的帮助,并且数据列应根据模式进行更改。那么您知道如何在使用模式修改数据表列的同时维护表的记录吗? class Program { public static DataTable toBeUpdatedTable; static void Main(string[] args) { // Deserialize the stored string path = "D:\\SerilaizedFile"

因此,现在您可能需要有关如何保留数据记录的帮助,并且数据列应根据模式进行更改。

那么您知道如何在使用模式修改数据表列的同时维护表的记录吗?
class Program
{
    public static DataTable toBeUpdatedTable;
    static void Main(string[] args)
    {
        // Deserialize the stored 
        string path = "D:\\SerilaizedFile";
        ReadMiscInfo(path);
        string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
        MySqlConnection connection = new MySqlConnection(connectionString);
        MySqlCommand command;
        connection.Open();

        DataSet set = new DataSet();
        try
        {
            command = connection.CreateCommand();
            command.CommandText = "select * from my_db.table";
            MySqlDataAdapter adapter = new MySqlDataAdapter(command);
            adapter.Fill(set);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        set.WriteXmlSchema("D:\\file.xsd");

        // Modify schema code 

        DataSet ds = new DataSet();
        ds.ReadXmlSchema("D:\\file.xsd");

        if (toBeUpdatedTable.Columns.Count > 0)
        {
            DataTable tempTable = ds.Tables[0];
            foreach (DataColumn col in tempTable.Columns)
            {
                if (!toBeUpdatedTable.Columns.Contains(col.ColumnName))
                    toBeUpdatedTable.Columns.Add(col.ColumnName, col.DataType);
            }
            Console.WriteLine("Table Updated");
        }
        else
        {
            toBeUpdatedTable = ds.Tables[0];
            Console.WriteLine("Table Created");
        }

        foreach (DataColumn col in toBeUpdatedTable.Columns)
        {
            Console.WriteLine(col.ColumnName + " type : " + col.DataType);
        }

        WriteMiscInfo(path);
        Console.ReadLine();
    }

    private static void WriteMiscInfo(string path)
    {
        if (path != null)
        {
            Stream s = new FileStream(path, FileMode.Create);
            IFormatter f2 = new BinaryFormatter();
            f2.Serialize(s, toBeUpdatedTable);
            s.Close();
        }
    }

    private static void ReadMiscInfo(string path)
    {
        if (File.Exists(path))
        {
            Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
            IFormatter f1 = new BinaryFormatter();
            try
            {
                toBeUpdatedTable = (DataTable)f1.Deserialize(s);
            }
            catch
            {
                s.Close();
                toBeUpdatedTable = new DataTable();
            }
            s.Close();
        }
        else
        {
            toBeUpdatedTable = new DataTable();
        }
    }
}
 class Program
    {
        static void Main(string[] args)
        {
            // Read a mysql table
            string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand command;
            connection.Open();

            DataSet set = new DataSet();
            try
            {
                command = connection.CreateCommand();
                //command.CommandText = "select * from toolsite.user limit 5";
                command.CommandText = "select FirstName, LastName, PersonID, City from my_db.Persons limit 5";
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                adapter.Fill(set);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }

            // Write Sxml Schema
            set.WriteXmlSchema("D:\\file.xsd");

            // Modify schema code 
            FileStream fs = new FileStream("D:\\file.xsd", FileMode.Open);
            XmlSchema schema;
            ValidationEventHandler eventHandler = new ValidationEventHandler(Program.ShowCompileErrors);
            try
            {             

                // Read the schema into an XMLSchema object.
                schema = XmlSchema.Read(fs, eventHandler);

                // Compile the schema.
                schema.Compile(eventHandler);

                // Define an XMLSchemaObjectTable to read the schema elements.
                // This schematable will contain a single element named "NewDataSet" according to mySql syntax
                XmlSchemaObjectTable schematable;
                schematable = schema.Elements;

                // Define a QualifiedName to identify the elements.
                XmlQualifiedName qname = new XmlQualifiedName("NewDataSet", "");

                XmlSchemaElement singleElement = new XmlSchemaElement();
                XmlSchemaComplexType complextype = new XmlSchemaComplexType();               

                //  See if the XmlSchemaObjectTable has the element.
                if (schematable.Contains(qname))
                {
                    singleElement = (XmlSchemaElement)schematable[qname];

                        if (singleElement.SchemaTypeName.ToString() == "")
                        {
                            complextype = (XmlSchemaComplexType)singleElement.SchemaType;

                            XmlSchemaChoice choice = new XmlSchemaChoice();
                            choice = (XmlSchemaChoice)complextype.Particle;

                            XmlSchemaObjectCollection collection = choice.Items;

                            XmlSchemaObject obj = collection[0];

                            XmlSchemaComplexType complexsubtype = (XmlSchemaComplexType)((XmlSchemaElement)obj).SchemaType;

                            XmlSchemaSequence seqelement = new XmlSchemaSequence();
                            seqelement = (XmlSchemaSequence)complexsubtype.Particle;

                            // Remove column here
                            XmlSchemaElement passwordElement = new XmlSchemaElement();
                            passwordElement = (XmlSchemaElement)seqelement.Items[1];
                            seqelement.Items.Remove(passwordElement);

                            // Add any new column here
                            XmlSchemaElement Newelement = new XmlSchemaElement();
                            Newelement.Name = "Country";
                            Newelement.MinOccurs = 0;

                            Newelement.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                            seqelement.Items.Add(Newelement);

                        }
                }

                // Display the schema in the Output window.
                schema.Write(Console.Out);

                Console.ReadLine();

                // Save the modified schema
                StreamWriter strmWrtr = new StreamWriter("D:\\newfile.xsd", false);
                schema.Write(strmWrtr);
                strmWrtr.Close();
            }
            catch (XmlSchemaException schemaEx)
            {
                Console.WriteLine(schemaEx.Message);
            }
            catch (XmlException xmlEx)
            {
                Console.WriteLine(xmlEx.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {                
                fs.Close();
            }

            // Read the modified schema
            // PROBLEM HERE : My schema is modified but my data of table is lost. I want to just modify the columns . 
            //I dont want to lose data
            set.ReadXmlSchema("D:\\newfile.xsd");

            foreach (DataColumn col in set.Tables[0].Columns)
            {
                Console.WriteLine(col.ColumnName + " type : " + col.DataType);
            }

            Console.WriteLine("Total records = " + set.Tables[0].Rows.Count);
            foreach (DataRow row in set.Tables[0].Rows)
            {
                for(int i = 0; i < row.ItemArray.Count(); i++)
                {
                    Console.Write(row.ItemArray[i] + " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }

        public static void ShowCompileErrors(object sender, ValidationEventArgs args)
        {
            Console.WriteLine("ERROR : Validation Error: {0}", args.Message);
        }
    }