views:

47

answers:

2

i try to add data with ExecuteCommand. everything is ok if list<string> data. i converted string type list into arraylist to add sql. But <MyClass> there are rows and column. look below picture.

  public class MyTally :  ISave
    {
        public List<ENG_MPD> engMpdIntersectList { get; set; }
        private ArrayList engMyTallyList;
        private EngDataContext engCtx;
        public MyTally()
        {
            engCtx = new EngDataContext();
            engMpdIntersectList = new List<ENG_MPD>();

        }
       #region ISave Members

        public bool Save()
        {

            engMyTallyList = new ArrayList(engMpdIntersectList);
            System.Windows.Forms.MessageBox.Show(engMyTallyList.Count.ToString());


              int rowsAffected = engCtx.ExecuteCommand("insert into ENG_MYTALLY Values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20}," +
                                                                                 "{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37},{38},{39},{40}," +
                                                                                 "{41},{42},{43},{44},{45},{46},{47},{48},{49},{50},{51},{52},{53},{54},{55},{56},{57},{58},{59},{60}," +
                                                                                 "{61},{62},{63},{64},{65},{66},{67},{68},{69},{70},{71},{72},{73},{74},{75},{76},{77},{78},{79},{80}," +
                                                                                 "{81},{82},{83},{84},{85},{86},{87},{88},{89},{90},{91},{92},{93},{94},{95},{96},{97},{98},{99},{100}," +
                                                                                 "{101},{102},{103},{104},{105},{106},{107},{108},{109},{110},{111},{112},{113},{114},{115},{116},{117},{118},{119},{120}," +
                                                                                 "{121},{122},{123},{124},{125},{126},{127},{128},{129},{130},{131},{132},{133},{134},{135},{136})",
                                                                                 engMyTallyList[0], engMyTallyList[1], engMyTallyList[2], engMyTallyList[3], engMyTallyList[4], engMyTallyList[5], engMyTallyList[6], engMyTallyList[7], engMyTallyList[8], engMyTallyList[9], engMyTallyList[10], engMyTallyList[11], engMyTallyList[12], engMyTallyList[13], engMyTallyList[14], engMyTallyList[15], engMyTallyList[16], engMyTallyList[17], engMyTallyList[18], engMyTallyList[19], engMyTallyList[20],
                                                                                 engMyTallyList[21], engMyTallyList[22], engMyTallyList[23], engMyTallyList[24], engMyTallyList[25], engMyTallyList[26], engMyTallyList[27], engMyTallyList[28], engMyTallyList[29], engMyTallyList[30], engMyTallyList[31], engMyTallyList[32], engMyTallyList[33], engMyTallyList[34], engMyTallyList[35], engMyTallyList[36], engMyTallyList[37], engMyTallyList[38], engMyTallyList[39], engMyTallyList[40],
                                                                                 engMyTallyList[41], engMyTallyList[42], engMyTallyList[43], engMyTallyList[44], engMyTallyList[45], engMyTallyList[46], engMyTallyList[47], engMyTallyList[48], engMyTallyList[49], engMyTallyList[50], engMyTallyList[51], engMyTallyList[52], engMyTallyList[53], engMyTallyList[54], engMyTallyList[55], engMyTallyList[56], engMyTallyList[57], engMyTallyList[58], engMyTallyList[59], engMyTallyList[60],
                                                                                 engMyTallyList[61], engMyTallyList[62], engMyTallyList[63], engMyTallyList[64], engMyTallyList[65], engMyTallyList[66], engMyTallyList[67], engMyTallyList[68], engMyTallyList[69], engMyTallyList[70], engMyTallyList[71], engMyTallyList[72], engMyTallyList[73], engMyTallyList[74], engMyTallyList[75], engMyTallyList[76], engMyTallyList[77], engMyTallyList[78], engMyTallyList[79], engMyTallyList[80],
                                                                                 engMyTallyList[81], engMyTallyList[82], engMyTallyList[83], engMyTallyList[84], engMyTallyList[85], engMyTallyList[86], engMyTallyList[87], engMyTallyList[88], engMyTallyList[89], engMyTallyList[90], engMyTallyList[91], engMyTallyList[92], engMyTallyList[93], engMyTallyList[94], engMyTallyList[95], engMyTallyList[96], engMyTallyList[97], engMyTallyList[98], engMyTallyList[99], engMyTallyList[100],
                                                                                 engMyTallyList[101], engMyTallyList[102], engMyTallyList[103], engMyTallyList[104], engMyTallyList[105], engMyTallyList[106], engMyTallyList[107], engMyTallyList[108], engMyTallyList[109], engMyTallyList[110], engMyTallyList[111], engMyTallyList[112], engMyTallyList[113], engMyTallyList[114], engMyTallyList[115], engMyTallyList[116], engMyTallyList[117], engMyTallyList[118], engMyTallyList[119], engMyTallyList[120],
                                                                                 engMyTallyList[121], engMyTallyList[122], engMyTallyList[123], engMyTallyList[124], engMyTallyList[125], engMyTallyList[126], engMyTallyList[127], engMyTallyList[128], engMyTallyList[129], engMyTallyList[130], engMyTallyList[131], engMyTallyList[132], engMyTallyList[133], engMyTallyList[134], engMyTallyList[135], engMyTallyList[136]);
             return rowsAffected > 0;
        }

        #endregion
    }

alt text

 engMyTallyList = new ArrayList(engMpdIntersectList);

engMpdIntersectList is an instance from myClass... how can i use it? my Arraylist values ENG_MPD type i need ENG_MPD columns values...

A: 

You could override the ToString() method in myClass to return the class data you want and then call engMyTallyList[i].ToString() for each item.

David Neale
A: 
engCtx.ExecuteCommand("insert into ENG_MYTALLY Values({0})", 
    string.Join(", ", engMyTallyList.Select(x => x.ToString()).ToArray())
);

Instead of x.ToString() you can call whatever method or property you like that returns string. If engMyTallyList is not generic, you will need to cast it before using Select.

engMyTallyList.Cast<MyTally>().Select(x => x.ToString()).ToArray()

Edit: You don't have to convert from List<ENG_MPD> to ArrayList to add it into SQL. Use

engMpdIntersectList.Select(x => x.ToString()).ToArray()

or if you have initially an ArrayList of ENG_MPD, use

engMyTallyList.Cast<ENG_MPD>().Select(x => x.ToString()).ToArray()

I initially thought of engMyTallyList to be a list of MyTally objects, not as they actually are ENG_MPD.

Draco Ater
i added your codes in my project but : 'Engineering.ENG_MPD' object of type 'Engineering.MyTally' type could not be discarded. int Test = engCtx.ExecuteCommand("insert into ENG_MYTALLY Values({0})", string.Join(", ", engMyTallyList.Cast<MyTally>().Select(x => x.ToString()).ToArray()));
Phsika
InvalidCastException...
Phsika
I have modified the answer. If that will not help, write some detaios of exception. (Where, what converts and into what)
Draco Ater