I have a bit of C# code in which am querying an Oracle database table (let's call it T1
) and also a SQL Server database table (let's call it T2
), and am placing the data from the T1 into a dictionary and also a list (I can use either). I am only adding the deptRef
from T1
into the dictionary/list.
What I am trying to achieve is checking if the data from T2
exists in the dictionary OR list in the SELECT
statement I am using e.g.
SELECT deptRef, deptName
FROM T2
WHERE deptRef IN List
So far I have this:
string strDeptName = "";
string strDeptRef = "";
string connstring = Convert.ToString(Dts.Variables["$Project::ORACLEConnectionString"].Value);
var sql = "SELECT d_reference AS DeptRef, d_name AS DeptName FROM T1 ORDER BY 1 ASC";
OleDbConnection t1con;
t1con = new OleDbConnection(connstring);
DataSet _ds = new DataSet();
DataTable _dt = new DataTable();
t1con.Open();
OleDbCommand cmd = new OleDbCommand(sql, t1con);
cmd.Connection = t1con;
cmd.CommandType = CommandType.Text;
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
strDeptRef = dr.GetString(0).ToString();
strDeptName = dr.GetString(1);
// ADD TO LIST
List<string> T1DeptsList = new List<string>();
foreach (string dept in T1DeptsList)
{
T1DeptsList.Add(dept);
}
// Add to DICTIONARY
Dictionary<string, int> T1DeptsDict = new Dictionary<string, int>();
int ii = 0;
T1DeptsDict.Add(strDeptRef, ii);
ii++;
}
// This is the second query
var sqlcon = Convert.ToString(Dts.Variables["$Project::SQLConnectionString"].Value);
OleDbConnection mwconnstring;
sqlconnstring = new OleDbConnection(sqlcon);
sqlconnstring.Open();
var mwsql = @"SELECT d_name FROM T2 WHERE d_reference IN T1DeptsList or T1DeptsDict....."; //this is the part where I'd like to query the List/Dictionary
OleDbCommand cmd2 = new OleDbCommand(mwsql, sqlconnstring);
cmd2.Connection = sqlconnstring;
cmd2.CommandText = mwsql;
cmd2.CommandType = System.Data.CommandType.Text;
The queries both work in that they return the data I expect but the part I'm having an issue with is referencing either the list OR the dictionary values. Either the list or the dictionary - it doesn't have to be both but just included them in case anyone advised which of the two is the more preferred.
Any help would be greatly appreciated.