Beispiel #1
0
    public static bool LoadFromCSVFile(string path, ref DataTable DT)
    {
        int DTColNum = DT.Columns.Count;

        OOFormArray form = new OOFormArray();

        form = OOFormArray.ReadFormCSVFile(path);
        int rowCnt = form.mRowCount;
        int colCnt = form.mColumnCount;

        //print("DTColNum" + DTColNum + " colCnt:" + colCnt + " rowCnt:" + rowCnt);

        if (DTColNum != colCnt)
        {
            print("Not Match!");
        }

        List <string> Keys      = new List <string>();
        bool          bSameKeys = true;

        for (int i = 0; i < colCnt; i++)
        {
            string key = form.GetString(i, 0);

            if (DT.Columns[i].ColumnName != key)
            {
                bSameKeys = false;
                break;
            }
            //print("ColumnName:" + DT.Columns[i].ColumnName + " Key:" + key);

            /* if(DT.Columns[i].ColumnName == key)
             * {
             *  print("Good Key:" + key);
             * }*/

            Keys.Add(key);
            //print("Key:" + key);
        }
        if (!bSameKeys)
        {
            return(false);
        }


        // read data
        for (int r = 1; r < rowCnt; r++)
        {
            DataRow dr = DT.NewRow();
            for (int c = 0; c < Keys.Count; c++)
            {
                System.Type tp = DT.Columns[c].DataType;
                if (tp == typeof(int))
                {
                    int val = form.GetInt(c, r);
                    dr[c] = val;
                    DebugPrint("Int:" + val);
                    // print("Set Int:" + val.ToString() + " OK?" + bSet.ToString());
                }
                else if (tp == typeof(Vector2))
                {
                    Vector2 val = form.GetVector2(c, r);
                    dr[c] = val;
                    DebugPrint("Vector2:" + val.ToString());
                }
                else if (tp == typeof(Vector3))
                {
                    Vector3 val = form.GetVector3(c, r);
                    dr[c] = val;
                    DebugPrint("Vector3:" + val.ToString());
                }
                else if (tp == typeof(Vector4))
                {
                    Vector4 val = form.GetVector4(c, r);
                    dr[c] = val;
                    DebugPrint("Vector4:" + val.ToString());
                }
                else if (tp == typeof(string))
                {
                    string val = form.GetString(c, r);
                    dr[c] = val;
                    DebugPrint("string:" + val);
                }
                else if (tp == typeof(float))
                {
                    float val = form.GetFloat(c, r);
                    dr[c] = val;
                    DebugPrint("float:" + val.ToString());
                }
                else if (tp == typeof(Rect))
                {
                    Rect val = form.GetRect(c, r);
                    dr[c] = val;
                    DebugPrint("Rect:" + val.ToString());
                }
                else if (tp == typeof(bool))
                {
                    bool val = form.GetBool(c, r);
                    dr[c] = val;
                    DebugPrint("Bool:" + val.ToString());
                }

                else
                {
                    DebugPrint("Invalid Datatype");
                }
            }
            DT.Rows.Add(dr);
            //PrintDataTable(DT);
        }
        int DTRowCnt =
            DT.Rows.Count;

        print("Load DataTable Rows:" + DTRowCnt.ToString());

        return(true);
    }