我想连接到多个数据库,并希望通过这些连接运行某些查询,但它无法正常工作。
string source = "10.0.0.0";
string user = "abc";
string password="abc";
DataTable dt = new DataTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
string source = dt.Rows[i][2].ToString();
string user = dt.Rows[i][1].ToString();
int password = Convert.ToInt32(user) + 111;
OracleConnection conn = new OracleConnection("Data Source = " + source + ": 1521/rms; User id = " + user + "; Password=" + password + ";");
conn.Open();
OracleCommand cmd = new OracleCommand(" SELECT SUM(AI_TRN) FROM tr_rtl where DC_DY_BSN = '06-04-2016'and mall like '%" + Mallname.Text + "%' ", conn);
cmd.ExecuteNonQuery();
OracleDataAdapter oda = new OracleDataAdapter(cmd);
oda.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
label1.Text = source;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
抱歉,我发布了错误的代码
答案 0 :(得分:0)
您可能需要做的是遍历连接列表并使用所有结果填充一个数据表。这是一个例子:
List<OracleConnections> Connections = new List<OracleConnections>();
DataTable FinalResults = new DataTable();
foreach (var Connection in Connections)
{
using (Connection)
{
DataTable TemporaryTable = new DataTable();
Connection.Open();
OracleCommand Command = new OracleCommand("SomeCommandText", Connection);
OracleDataAdapter Adapter = new OracleDataAdapter(Command);
Adapter.Fill(TemporaryTable);
FinalResults.Merge(TemporaryTable);
}
}
这应该在一个最终表(结果)中为您提供每个数据库/连接的所有结果,或者如果每个连接中的列/数据不同,您可以执行数据集。
更新以解释上面列出的评论。
string Source = "10.0.0.0";
string User = "abc";
string Password = "abc";
DataTable dt = new DataTable();
// Remove this or use another reference (different table), you just created the table ^, it has no rows. -> "for (int i = 0; i < dt.Rows.Count; i++)"