不编译DataColumn中的产品表达式,显示错误为“找不到列[max]”。

时间:2012-10-29 21:37:33

标签: mysql c#-4.0 ado.net

运行代码时出现错误“找不到列[max] 。”但我已将max和min列添加到数据集中的表

MySql.Data.MySqlClient.MySqlConnection mycon = new MySqlConnection(GetConnectionString());
    if (mycon.State != ConnectionState.Open)

        {

            string sqlCat = "SELECT * FROM out_of_mark_table";
            string sqlProd = "SELECT * FROM scord_mark_table";

            MySqlDataAdapter da = new MySqlDataAdapter(sqlCat, mycon);
            DataSet ds = new DataSet();

            try
            {
                mycon.Open();
                da.Fill(ds, "out_of_mark_table");

                da.SelectCommand.CommandText = sqlProd;
                da.Fill(ds, "scord_mark_table");
            }
            finally
            {
                mycon.Close();
            }

            DataRelation relat = new DataRelation("CatProds", ds.Tables["out_of_mark_table"].Columns["test_id"], ds.Tables["scord_mark_table"].Columns["test_id"]);
            ds.Relations.Add(relat);

            DataColumn count = new DataColumn("Products (#)", typeof(int), "COUNT(Child(CatProds).test_id)");
            DataColumn max = new DataColumn("Most Expensive Product", typeof(decimal), "MAX(Child(CatProds).total)");
            DataColumn min = new DataColumn("Least Expensive Product", typeof(decimal), "MIN(Child(CatProds).total)");
            DataColumn no=new DataColumn("No");
            DataColumn IdCol = new DataColumn();
            min.Caption = "min";
            max.Caption = "max";
            string expr = "max * min";

            IdCol.ColumnName = "ID";
            IdCol.DataType = Type.GetType("System.Int32");
            IdCol.ReadOnly = true;
            IdCol.AllowDBNull = false;
            //IdCol.Unique = true;
            IdCol.AutoIncrement = true;
            IdCol.AutoIncrementSeed = 1;
            IdCol.AutoIncrementStep = 1;



            ds.Tables["out_of_mark_table"].Columns.Add(count);
            ds.Tables["out_of_mark_table"].Columns.Add(max);
            ds.Tables["out_of_mark_table"].Columns.Add(min);
            ds.Tables["out_of_mark_table"].Columns.Add(IdCol);
            DataColumn sum = new DataColumn("Sum of", typeof(int), expr, MappingType.Attribute);
            **ds.Tables["out_of_mark_table"].Columns.Add(sum);**
            IdCol.SetOrdinal(0);
            GridView1.DataSource = ds.Tables["out_of_mark_table"];
            GridView1.DataBind();

1 个答案:

答案 0 :(得分:1)

您已将字幕设置为“最大”和“最小”,但DataColumns's标识符为OrdinalColumnName。您已将ColumnName通过constructor设置为

"Most Expensive Product""Least Expensive Product"

所以使用

string expr = "[Most Expensive Product] * [Least Expensive Product]";

代替。