我有两个数据表,我需要匹配每个数据表的邮政编码,每次程序找到匹配项时,我都需要将匹配行中的列设置为字符串。这是我的代码。
我不知道在foreach循环中该做什么,或者如果有更简单的方法可以做到这一点,请说出来。
//Datatable for entire list of zipcodes
DataTable datat = new DataTable("DATA");
DataColumn state = new DataColumn();
DataColumn county = new DataColumn();
DataColumn zipcode = new DataColumn();
DataColumn latitude = new DataColumn();
DataColumn longitude = new DataColumn();
DataColumn salesperson = new DataColumn();
DataColumn originalrec = new DataColumn();
//Add the columns to the datatable
datat.Columns.Add(state);
datat.Columns.Add(county);
datat.Columns.Add(zipcode);
datat.Columns.Add(latitude);
datat.Columns.Add(longitude);
datat.Columns.Add(salesperson);
datat.Columns.Add(originalrec);
return datat;
}
private static DataTable InitData2()
{
//Datatable for entire list of zipcodes
DataTable datat2 = new DataTable("DATA");
DataColumn ctype = new DataColumn();
DataColumn csalesperson = new DataColumn();
DataColumn czipcode = new DataColumn();
//Add the columns to the datatable
datat2.Columns.Add(ctype);
datat2.Columns.Add(csalesperson);
datat2.Columns.Add(czipcode);
return datat2;
}
private static String InitPath()
{
string path = "C:/Documents and Settings/Andre/Desktop/SalesMap/data.csv";
return path;
}
private static String InitPath2()
{
string path2 = "C:/Documents and Settings/Andre/Desktop/SalesMap/CUSTOMER_PROSPECT_SALESPERSON.csv";
return path2;
}
public static void Main()
{
try
{
DataTable dt = InitData1();
DataTable dt2 = InitData2();
StreamReader sr = new StreamReader(InitPath());
StreamReader sr2 = new StreamReader(InitPath2());
String csvData = string.Empty;
String csvData2 = string.Empty;
while ((csvData = sr.ReadLine()) != null)
{
String[] data = csvData.Split(',');
//dt.Rows.Add(data);
DataRow newRow1 = dt.NewRow();
newRow1[0] = data[0].ToString();
newRow1[1] = data[1].ToString();
newRow1[2] = data[2].ToString();
newRow1[3] = data[3].ToString();
newRow1[4] = data[4].ToString();
newRow1[5] = "";
newRow1[6] = "";
dt.Rows.Add(newRow1);
Console.WriteLine("Row added for: dt");
}
dt.WriteXml(@"c:\test\dt1.xml");
sr.Close();
while ((csvData2 = sr2.ReadLine()) != null)
{
String[] data2 = csvData2.Split(',');
DataRow newRow2 = dt2.NewRow();
newRow2[0] = data2[0].ToString();
newRow2[1] = data2[1].ToString();
newRow2[2] = data2[2].ToString();
dt2.Rows.Add(newRow2);
Console.WriteLine("Row added for: dt2");
}
dt2.WriteXml(@"c:\test\dt2.xml");
sr2.Close();
foreach (DataRow row1 in dt.Rows)
{
//Dont know what to do here
}
}
catch (Exception e)
{
//Console.WriteLine("The files could not be read:");
//Console.WriteLine(e.Message);
StreamWriter sw = new StreamWriter(@"c:\test\error.txt");
sw.WriteLine(e.Message);
sw.Close();
}
}
}
}