将数据表保存在文本中并稍后查找

时间:2013-02-21 06:21:32

标签: c# arrays datatable lookup

我有一个与C#做的功课,计算一些东西,保存在数据库(或文本文件)中,然后查找。这是我的代码。我试过但似乎没有用。任何人都可以帮我检查一下吗?非常感谢你。

private void button1_Click(object sender, EventArgs e)
{
    int l0 = 210;
    int l1 = 440;
    int l2 = 330;
    int l3 = 435;
    DataTable workspace = new DataTable();
    workspace.Columns.Add("Xd", typeof(double));
    workspace.Columns.Add("Yd", typeof(double));
    workspace.Columns.Add("Zd", typeof(double));
    workspace.Columns.Add("Th1", typeof(double));
    workspace.Columns.Add("Th2", typeof(double));
    workspace.Columns.Add("Th3", typeof(double));
    workspace.Columns.Add("Th4", typeof(double));
    workspace.Columns.Add("Th5", typeof(double));
    workspace.Columns.Add("Th6", typeof(double));
    workspace.Columns.Add("Th7", typeof(double));

    int th1 = 0, th2 = 0, th3 = -90, th4 = -45, th5 = -130, th6 = -130, th7 = -45;
    while (th1 <= 180)
    {
        double t1 = th1 * Math.PI / 180;
        while (th2 <= 180)
        {
            double t2 = th1 * Math.PI / 180;
            while (th3 <= 90)
            {
                double t3 = th3 * Math.PI / 180;
                while (th4 <= 45)
                {
                    double t4 = th4 * Math.PI / 180;
                    while (th5 <= 130)
                    {
                        double t5 = th5 * Math.PI / 180;
                        while (th6 <= 130)
                        {
                            double t6 = th6 * Math.PI / 180;
                            while (th7 <= 45)
                            {
                                double t7 = th7 * Math.PI / 180;
                                double Xd = l3 * (Math.Cos(t6) * (Math.Sin(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) + Math.Cos(t1) * Math.Cos(t4) * Math.Sin(t2)) + Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) - Math.Cos(t1) * Math.Sin(t2) * Math.Sin(t4)) + Math.Sin(t5) * (Math.Cos(t3) * Math.Sin(t1) - Math.Cos(t1) * Math.Cos(t2) * Math.Sin(t3)))) + l0 * Math.Cos(t1) + l2 * (Math.Sin(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) + Math.Cos(t1) * Math.Cos(t4) * Math.Sin(t2)) + l1 * Math.Cos(t1) * Math.Sin(t2);
                                double Yd = l0 * Math.Sin(t1) - l3 * (Math.Cos(t6) * (Math.Sin(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) - Math.Cos(t4) * Math.Sin(t1) * Math.Sin(t2)) + Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) + Math.Sin(t1) * Math.Sin(t2) * Math.Sin(t4)) + Math.Sin(t5) * (Math.Cos(t1) * Math.Cos(t3) + Math.Cos(t2) * Math.Sin(t1) * Math.Sin(t3)))) - l2 * (Math.Sin(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) - Math.Cos(t4) * Math.Sin(t1) * Math.Sin(t2)) + l1 * Math.Sin(t1) * Math.Sin(t2);
                                double Zd = l3 * (Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t2) * Math.Sin(t4) + Math.Cos(t3) * Math.Cos(t4) * Math.Sin(t2)) - Math.Sin(t2) * Math.Sin(t3) * Math.Sin(t5)) - Math.Cos(t6) * (Math.Cos(t2) * Math.Cos(t4) - Math.Cos(t3) * Math.Sin(t2) * Math.Sin(t4))) - l2 * (Math.Cos(t2) * Math.Cos(t4) - Math.Cos(t3) * Math.Sin(t2) * Math.Sin(t4)) - l1 * Math.Cos(t2);

                                textBoxX.Text = Xd.ToString();
                                textBoxY.Text = Yd.ToString();
                                textBoxZ.Text = Zd.ToString();
                                workspace.Rows.Add(Xd, Yd, Zd, t1, t2, t3, t4, t5, t6, t7);
                                th7 = th7 + 5;
                            }
                            th6 = th6 + 5;
                        }
                        th5 = th5 + 5;
                    }
                    th4 = th4 + 5;
                }
                th3 = th3 + 5;
            }
            th2 = th2 + 5;
        }
        th1 = th1 + 5;
    }

}

当我运行下面的应用程序时,它有一个问题: XML文档中存在错误(12504158,75)。我做错了什么?这个错误意味着什么?再次谢谢你。

        //Reading saved data
        var dtDeserializer = new XmlSerializer(typeof(DataTable));
        var data = (DataTable)dtDeserializer.Deserialize(File.Open("out.xml", FileMode.Open));

        DataRow[] result = data.Select("Xd= '100' AND Yd = '1000' AND Zd = '0'");
        foreach (DataRow row in result)
        {
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7} {8}, {9}", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]);
        }

        Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

只需序列化/反序列化您的DataTable,这里是一些示例代码:

        //Saving the DataTable
        workspace.TableName = "SomeName"; //A name is required in order to save the datable
        var dtSerializer = new XmlSerializer(typeof(DataTable));
        dtSerializer.Serialize(File.Create("out.xml"), workspace);

        //Reading saved data
        var dtDeserializer = new XmlSerializer(typeof(DataTable));
        var data = (DataTable)dtDeserializer.Deserialize(File.Open("out.xml", FileMode.Open));