从 2 个不同的数据库中获取所有需要的数据

Fetch all needed data from 2 different databases

[更新] 我遇到了一种情况,我需要从 2 个不同的数据库中获取数据,然后将其组合到一个模型中。我在这里创建了一个 API 方法来处理这个问题,但是当我开始使用第二个数据库时,我对如何检索多个项目感到非常困惑。我会解释。这是该方法的代码:

private List<FidelityModel> models;
public List<FidelityModel> getFidelityInfo2(string jobID) {
        FidelityModel fidelityInfo;
        SqlCommand command;
        SqlConnection conn;
        SqlDataReader reader;
        string cjsJobName, ipwNumber, overnight, site;
        int packageCount;
        DateTime sla;
        models = new List<FidelityModel>();
        using (conn = new SqlConnection(@"server = [servername]; database = Dropoff; Integrated Security = true;")) {
            conn.Open();
            command = new SqlCommand("SELECT " +
                "[Job Name], " +
                "[Job ID], " +
                "[Package Count], " +
                "[Ship Method] " +
                "FROM [cjs_data] " +
                "WHERE [File Name] LIKE '%FDY%' AND [JOB ID] = @jobID", conn);
            command.Parameters.Add("@jobID", SqlDbType.VarChar);
            command.Parameters["@jobID"].Value = jobID;
            //restructure to assign search results to string to later assign to model, as we will search again for SLA in a different database
            using (reader = command.ExecuteReader()) {
                if (reader.HasRows) {
                    while (reader.Read()) {
                        fidelityInfo = new FidelityModel();
                        fidelityInfo.cjsJobName = reader.GetString(0);
                        fidelityInfo.ipwNumber = reader.GetString(1);
                        fidelityInfo.packageCount = reader.GetInt32(2);
                        if (fidelityInfo.cjsJobName.Contains("OVN")) { fidelityInfo.overnight = "Yes"; } else {
                            fidelityInfo.overnight = (reader.GetString(3).Equals("Overnight")) ? "Yes" : "No";
                        }
                        //site = (cjsJobName.Contains("EDG")) ? "EDGEWOOD" : "Other Site"; //not always the case
                        fidelityInfo.site = "EDGEWOOD";
                        models.Add(fidelityInfo);
                    }
                }
            }
        }
        //How to incorporate this following block of code into the same model?
        using (conn = new SqlConnection(@"server = [servername]; database = MustMail; Integrated Security = true;")) {
            conn.Open();
            command = new SqlCommand("SELECT [SLA] FROM [Job] WHERE [JobID] = @jobID", conn);
            command.Parameters.Add("@jobID", SqlDbType.VarChar);
            command.Parameters["@jobID"].Value = jobID;
            using (reader = command.ExecuteReader()) {
                if (reader.HasRows) {
                    while (reader.Read()) {
                        //fidelityInfo.sla = reader.GetDateTime(0);
                    }
                }
            }
        }
        return models;
    }

正如您现在所见,我只是在不获取 SLA 的情况下运行它,因为我不知道如何实际将从第二个数据库获取的结果添加到同一模型中。

为 DataReader 中的每一行创建一个新的 FidelityModel 并将其添加到列表中。类似于:

while (reader.Read()) 
{
  var m = new FidelityModel()
  {
   cjsJobName = reader.GetString(0),
   ipwNumber = reader.GetString(1),
   packageCount = reader.GetInt32(2),
   sla = DateTime.Now
  };
  if (m.cjsJobName.Contains("OVN")) 
  { 
    m.overnight = "Yes"; 
  } 
  else 
  {
    m.overnight = (reader.GetString(3).Equals("Overnight")) ? "Yes" : "No";
  }
  models.Add(m);
}