如何将具有新行的数组插入数据库表

时间:2019-02-15 23:43:18

标签: c# mysql

我正在尝试将大小为2000的数组插入到包含13列的数据库表中,从第一行中的array [0]到array [12],然后在第二行中从array [13]到array [25]行..... 这是包含列名称的命令,但是我无法获取如何安排for循环插入表的位置,在我的尝试下,它不起作用。 我已经搜索了互联网,但找不到并理解正确的方法,谢谢您的助手

string shpttrackingSql = ("INSERT INTO shpttrackingTable Values (@Checkpoint, @Stn, @Location," +
"@DateTime, @Pcs, @Route, @Cycle, @Stat, @Pgln, @Count, @Last, @Remarks, @Comments)");

// Inserting data from array to Tracking Table
SqlCommand shpttrackingCmd = new SqlCommand(shpttrackingSql, con);

for (int i=0;i<2000;i++)
            {
                shpttrackingCmd.Parameters.AddWithValue("@Checkpoint", trackingArray[i]); //Checkpoint
                shpttrackingCmd.Parameters.AddWithValue("@Stn", trackingArray[i]); //Stn
                shpttrackingCmd.Parameters.AddWithValue("@Location", trackingArray[i]); //Location
                shpttrackingCmd.Parameters.AddWithValue("@DateTime", trackingArray[i]); //Date and Time
                shpttrackingCmd.Parameters.AddWithValue("@Pcs", trackingArray[i]); //Pieces 
                shpttrackingCmd.Parameters.AddWithValue("@Route", trackingArray[i]); //Route
                shpttrackingCmd.Parameters.AddWithValue("@Cycle", trackingArray[i]); //Cycle
                shpttrackingCmd.Parameters.AddWithValue("@Stat", trackingArray[i]); //Stat
                shpttrackingCmd.Parameters.AddWithValue("@Pgln", trackingArray[i]); 
                shpttrackingCmd.Parameters.AddWithValue("@Count", trackingArray[i]); 
                shpttrackingCmd.Parameters.AddWithValue("@Last", trackingArray[i]); 
                shpttrackingCmd.Parameters.AddWithValue("@Remarks", trackingArray[i]); //Remarks
                shpttrackingCmd.Parameters.AddWithValue("@Comments", trackingArray[i]); //Comments

                con.Open();
                shpttrackingCmd.ExecuteNonQuery();
                con.Close();
            }

1 个答案:

答案 0 :(得分:0)

我已经通过添加解决了循环中的参数     shpttrackingCmd.Parameters.Clear();如下所示

for (int i=13;i<53;i+=13)
            {
                str = trackingArray[i+0];
                str = str.Replace("\r\n                </a>", "").Trim();
                substr = str.Substring(str.Length - 30).Trim();
                shpttrackingCmd.Parameters.AddWithValue("@Checkpoint", substr); //Checkpoint

                shpttrackingCmd.Parameters.AddWithValue("@Stn", trackingArray[i+1]); //Stn
                shpttrackingCmd.Parameters.AddWithValue("@Location", trackingArray[i+2]); //Location
                shpttrackingCmd.Parameters.AddWithValue("@DateTime", trackingArray[i+3]); //Date and Time
                shpttrackingCmd.Parameters.AddWithValue("@Pcs", trackingArray[i+4]); //Pieces 
                shpttrackingCmd.Parameters.AddWithValue("@Route", trackingArray[i+5]); //Route
                shpttrackingCmd.Parameters.AddWithValue("@Cycle", trackingArray[i+6]); //Cycle
                shpttrackingCmd.Parameters.AddWithValue("@Stat", trackingArray[i+7]); //Stat
                shpttrackingCmd.Parameters.AddWithValue("@Pgln", trackingArray[i+8]); 
                shpttrackingCmd.Parameters.AddWithValue("@Count", trackingArray[i+9]); 
                shpttrackingCmd.Parameters.AddWithValue("@Last", trackingArray[i+10]); 
                shpttrackingCmd.Parameters.AddWithValue("@Remarks", trackingArray[i+11]); //Remarks
                shpttrackingCmd.Parameters.AddWithValue("@Comments", trackingArray[i+12]); //Comments

                con.Open();
                shpttrackingCmd.ExecuteNonQuery();
                con.Close();

                shpttrackingCmd.Parameters.Clear();
            }