c#sqlcommand在foreach循环中的同一次迭代中触发两次

时间:2018-05-14 07:39:00

标签: c# sql-server ssis

我正在读取输入表中的行以及调用api的每一行并在输出表中插入响应。每一行在输入表中都是唯一的,因此是api响应,但是当我将它写入输出表时,行是重复的.Below是完整的代码

 string sqlconn, sqlquery;
            DataTable datatable = new DataTable();
            public void Main()
            {
                // TODO: Add your code here

                try
                {
                    sqlconn = Dts.Variables["User::varSqlconn"].Value.ToString();
                    sqlquery = Dts.Variables["User::varSqlquery"].Value.ToString();

                    using (SqlConnection sqlConn = new SqlConnection(sqlconn))
                    {

                        using (SqlCommand cmd = new SqlCommand(sqlquery, sqlConn))
                        {
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            da.Fill(datatable);
                        }
                    }
                    foreach (DataRow record in datatable.Rows)
                    {
                        APIAddress.CallGoogleAPI((int)record[0], (string)record[1]);
/*the problem begins here
                        using (SqlConnection connection = new SqlConnection(sqlconn))
                        {
                           if (APIAddress.FORMATTED_ADDRESS != null)
                            {
                                using (SqlCommand command = new SqlCommand(cmdStr, connection))
                                {
                                    connection.Open();
                                    command.Parameters.AddWithValue("@ADDR_ID", APIAddress.ADDRESS_ID);
                                    command.Parameters.AddWithValue("@ADDR_ONE", APIAddress.FORMATTED_ADDRESS != null ? APIAddress.FORMATTED_ADDRESS : DBNull.Value.ToString());
                                    command.Parameters.AddWithValue("@POSTALCODE", APIAddress.POSTALCODE_S != null ? APIAddress.POSTALCODE_S : DBNull.Value.ToString());
                                    command.ExecuteNonQuery();
                                    command.Parameters.Clear();
                                }
                            }

                            else
                            {
                            }
                        }
                   }               
                }
                catch (Exception ex)
                {
                    Dts.Events.FireError(0, "ScriptTask", ex.Message, null, 0);
                }

0 个答案:

没有答案