2013-02-21 30 views
0

我有功课要做与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(); 
+0

有没有在你的代码,保存数据。可以序列化DataTable或设置数据绑定 – Martheen 2013-02-21 06:29:37

回答

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)); 
+0

非常感谢您的帮助。它非常有帮助。 – 2013-02-22 04:36:39

+1

@ nobita_bk06:如果有帮助,请不要忘记标记为答案。 – 2013-06-23 16:39:33

相关问题