如何将list <myclass>数据添加到sqldatabase?</myclass>

时间:2010-07-14 14:39:45

标签: c# .net visual-studio-2008 linq linq-to-sql

我尝试使用ExecuteCommand添加数据。如果列表&lt; string&gt ;,一切都好数据。我将字符串类型列表转换为arraylist以添加sql。但是&lt; MyClass&gt;有行和列。看下面的图片。

  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 http://i30.tinypic.com/2435dg0.png

 engMyTallyList = new ArrayList(engMpdIntersectList);

engMpdIntersectList是myClass的一个实例...我该如何使用它?我的Arraylist值ENG_MPD类型我需要ENG_MPD列值......

2 个答案:

答案 0 :(得分:0)

您可以覆盖ToString()中的myClass方法以返回所需的班级数据,然后为每个项目调用engMyTallyList[i].ToString()

答案 1 :(得分:0)

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

您可以调用您喜欢的任何返回字符串的方法或属性,而不是x.ToString()。 如果engMyTallyList不是通用的,则需要在使用Select之前进行投射。

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

修改 您无需将List<ENG_MPD>转换为ArrayList即可将其添加到SQL。使用

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

或如果您最初ArrayList ENG_MPD,请使用

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

我最初认为engMyTallyList是MyTally对象的列表,而不是它们ENG_MPD