ds.Merge 与 ds.Tables[0].Merge 的用法

时间:2021-11-25 13:57:41

DataSet ds = new DataSet();

SqlConnection conn = new SqlConnection(ConnectionStr);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 180;

SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@strNum",SqlDbType.NVarChar,50),
                new SqlParameter("@strDjStartDate",SqlDbType.NVarChar,10),
                new SqlParameter("@strDjEndDate",SqlDbType.NVarChar,10),
                new SqlParameter("@strScStartDate",SqlDbType.NVarChar,10),
                new SqlParameter("@strScEndDate",SqlDbType.NVarChar,10),
                new SqlParameter("@strCreator",SqlDbType.NVarChar,50),
                new SqlParameter("@str_tb",SqlDbType.NVarChar,50),
               };

////1
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields]";

try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t1");
                    cmd.Parameters.Clear();
                    ds.Merge(dataSet, false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

////2
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields_12]";
                try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t2");
                    cmd.Parameters.Clear();
                    ds.Tables[0].Merge(dataSet.Tables[0], false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

////3
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields_13]";
                try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t3");
                    cmd.Parameters.Clear();
                    ds.Tables[0].Merge(dataSet.Tables[0], false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

////4
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields_14]";
                try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t4");
                    cmd.Parameters.Clear();
                    ds.Tables[0].Merge(dataSet.Tables[0], false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

////5
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields_15]";
                try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t5");
                    cmd.Parameters.Clear();
                    ds.Tables[0].Merge(dataSet.Tables[0], false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

////6
                parameters[0].Value = strNum;
                parameters[1].Value = strDjStartDate;
                parameters[2].Value = strDjEndDate;
                parameters[3].Value = strScStartDate;
                parameters[4].Value = strScEndDate;
                parameters[5].Value = strCreator;
                parameters[6].Value = "[auto_ZYQA-P-07-06_myFields_16]";
                try
                {
                    PrepareCommand(cmd, conn, null, CommandType.StoredProcedure, "Proc_Export8DPZYC", parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "t6");
                    cmd.Parameters.Clear();
                    ds.Tables[0].Merge(dataSet.Tables[0], false, MissingSchemaAction.Add);
                }
                catch
                {
                    conn.Close();
                    throw;
                }