我正在尝试比较来自不同日期的两个数据库。我想将ending
数据库中的“订单”表加载到DataTable
,然后将DataTable
转换为Dictionary<string,int>
,其中string = OrderNumber,int = ClientNumber。< / p>
一旦我将字典放入内存,我将运行我的函数,该函数将发送到beginning
数据库并执行atrOrdersEND.ContainsKey(ordernumber)
检查。如果存在则不会执行任何操作,如果它不存在,我希望将DataRow
的信息保存在某处。
这是解决这个问题的最好方法吗?
如果没有,我该怎么办?
static public DataTable getDatabaseInfo(string DB)
{
DB = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DB;
OleDbConnection conn = new OleDbConnection(DB);
string query = "SELECT * FROM ORDERS;";
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
conn.Open();
try
{
da.Fill(dt);
}
catch (OleDbException ex)
{
}
return dt;
}
static Dictionary<string,int> ToDictionary(DataTable DT)
{ //this doesn't work, I get the 'An item with the same key has already been added' error
var Dict = (from order in DT.AsEnumerable()
select new
{
OrderNumber = order.Field<string>("OrderNumber"),
ClientNumber = order.Field<int>("Client")
}).AsEnumerable()
.ToDictionary(k => k.OrderNumber, v => v.ClientNumber);
return Dict;
}
static public void CheckHistoryBEG(string DB)
{
DB = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DB;
OleDbConnection conn = new OleDbConnection(DB);
string query = "SELECT * " +
"FROM Orders";
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
DataTable dt = new DataTable();
try
{
adapter.Fill(dt);
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
string ordernumber = dt.Rows[i]["OrderNumber"].ToString();
if (atrOrdersEND.ContainsKey(ordernumber))
{
atrOrdersEND[ordernumber] = atrOrdersEND[ordernumber] + 1;
}
else
{
atrOrdersEND.Add(ordernumber, 1);
}
}
}
catch (OleDbException ex)
{
}
}
public Main()
{
DataTable EndDB_Data = getDatabaseInfo(endDBLocation);
Dictionary<string, int> atrOrdersBEG = ToDictionary(EndDBData);
CheckHistoryBEG(beginningDBLocation);
}
答案 0 :(得分:0)
您可以通过在MS Access数据库中使用链接表来避免编写任何C#代码。从一个DB,您可以引用另一个数据库中的表。然后,您可以在两个数据库之间进行连接或存在查询。