我之前从未做过这样的事情,如果我只传递一个整数就行,但是我有一个我需要用于POSITION_ID的列表。该列表例如仅1,2,3..etc。这会从The specified object must not be an instance of a generic type
引发异常ExecuteParameterQuery
。我不确定在涉及列表时是否/如何执行此操作。
通过帮助进行的更改。
public List<Dictionary<string, string>> SelectedLineHistory(string partProgram, string machineId, List<int> Line2, bool orderByDate = false)
{
List<Dictionary<string, string>> results = new List<Dictionary<string, string>>();
string query = "SELECT MACHINE_ID, PART_PROGRAM, POSITION_ID, DATE_TIME, SAVE_DATE_TIME,DWELL_STIME, ADJUSTED_AMOUNT, DWELL_OFFSET, EMPL_ID " +
"FROM MPCS.WJ_DWELL_OFFSETS_HIST " +
"WHERE PART_PROGRAM = ? and MACHINE_ID = ? and POSITION_ID = ? ";
if (orderByDate)
{
query += " order by save_date_time desc, position_id asc";
}
List<OleDbParameter> queryParameters = new List<OleDbParameter>();
foreach (var line in Line2)
{
queryParameters.Clear();
queryParameters.Add(new OleDbParameter("PART_PROGRAM", partProgram));
queryParameters.Add(new OleDbParameter("MACHINE_ID", machineId));
queryParameters.Add(new OleDbParameter("POSITION_ID", Line));
results = this.ExecuteParameterQuery(query, queryParameters);
}
return (results);
}
如果我不使用results = this.ExecuteParameterQuery(query, queryParameters);
代替results.add
,我会收到错误,但我不认为这是我的问题。
它仍会产生原始错误The specified object must not be an instance of a generic type
这就是我称之为的方式。在需要使用positionID列表
之前,这又有效我还在努力弄清楚我要做什么&#39;新的&#39;正如所建议的那样,但尚未发现它,但希望保持更新。
public void SelectedHistory(bool showMessages = false)
{
List<Dictionary<string, string>> resultHistory = new List<Dictionary<string, string>>();
string machineId = cboMachine.Text.Trim().ToUpper();
string partProg = cboPartPrograms.Text.Trim().ToUpper();
lvViewHistory.Items.Clear();
if (!String.IsNullOrEmpty(machineId) && !String.IsNullOrEmpty(partProg))
{
resultHistory = oracleConnection.SelectedLineHistory(partProg, machineId, Line2, true);
if (resultHistory != null && resultHistory.Count > 0)
{
foreach (Dictionary<string, string> d in resultHistory)
{
ListViewItem lvi = new ListViewItem(d["MACHINE_ID"]);
lvi.SubItems.Add(d["PART_PROGRAM"]);
lvi.SubItems.Add(d["POSITION_ID"]);
lvi.SubItems.Add(d["DATE_TIME"]);
lvi.SubItems.Add(d["SAVE_DATE_TIME"]);
lvi.SubItems.Add(d["DWELL_STIME"]);
lvi.SubItems.Add(d["ADJUSTED_AMOUNT"]);
lvi.SubItems.Add(d["DWELL_OFFSET"]);
lvi.SubItems.Add(d["EMPL_ID"]);
lvViewHistory.Items.Add(lvi);
}
}
else
{
MessageBox.Show("No History Found");
}
}
else
{
MessageBox.Show("Please select Machine_Id and Part Program");
}
lvViewHistory.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
无论出于何种原因它现在都有效,但是不是给我行结果,而是在行2中有很多行号,它只是给我从最后一行号得到的结果。
因此,如果Line2有1,2,3我应该从行号为1 2和3的所有行获得sql结果,而不是我得到行号为3的结果。
答案 0 :(得分:2)
我认为您正在寻找以下内容。请注意,它将每个迭代的结果添加到result
列表中。它还纠正了@omniowner先前提到的缺陷。
public List<Dictionary<string, string>> SelectedLineHistory(string partProgram, string machineId, List<int> Line2, bool orderByDate = false)
{
List<Dictionary<string, string>> results;
string query = "";
List<OleDbParameter> queryParameters = new List<OleDbParameter>();
foreach (var line in Line2)
{
query = "SELECT MACHINE_ID, PART_PROGRAM, POSITION_ID, DATE_TIME, SAVE_DATE_TIME,DWELL_STIME, ADJUSTED_AMOUNT, DWELL_OFFSET, EMPL_ID ";
query += "FROM MPCS.WJ_DWELL_OFFSETS_HIST ";
query += "WHERE PART_PROGRAM = ? and MACHINE_ID = ? and POSITION_ID = ?";
if (orderByDate)
{
query += " order by save_date_time desc, position_id asc";
}
queryParameters.Add(new OleDbParameter("PART_PROGRAM", partProgram));
queryParameters.Add(new OleDbParameter("MACHINE_ID", machineId));
/* in the following parameter, use this iteration's value, line, *
* rather than the entire collection, Line2 */
queryParameters.Add(new OleDbParameter("POSITION_ID", line));
/**** this looks sketchy -- check it *****/
results.Add(this.ExecuteParameterQuery(query, queryParameters));
}
return results;
}
深思熟虑:如上所述,这将为Line2列表中的每个元素执行一次数据库查询/往返。您可能更喜欢只使用where子句执行一个查询,该子句指定Line2中的每个元素。这样会更有效率,但需要更多代码来准备返回此例程调用者的结果。
答案 1 :(得分:0)
在我看来,问题是你试图将整个List of Integers作为参数传递给...
queryParameters.Add(new OleDbParameter("POSITION_ID", Line2));
您应该尝试将其更改为您在列表中迭代的项目。像这样......
queryParameters.Add(new OleDbParameter("POSITION_ID", line));
在完成整个列表的迭代之前,您也会返回(正如STLDeveloper在问题评论中提到的那样)。您希望将该return语句移出foreach循环
答案 2 :(得分:0)
借用STL +1
public List<Dictionary<string, string>>
SelectedLineHistory(string partProgram, string machineId,
List<int> Line2, bool orderByDate = false)
{
List<Dictionary<string, string>> results = new List<Dictionary<string, string>>();
string query = "SELECT MACHINE_ID, PART_PROGRAM, POSITION_ID, DATE_TIME, SAVE_DATE_TIME,DWELL_STIME, ADJUSTED_AMOUNT, DWELL_OFFSET, EMPL_ID " +
"FROM MPCS.WJ_DWELL_OFFSETS_HIST " +
"WHERE PART_PROGRAM = ? and MACHINE_ID = ? and POSITION_ID = ? ";
if (orderByDate)
{
query += " order by save_date_time desc, position_id asc";
}
List<OleDbParameter> queryParameters = new List<OleDbParameter>();
foreach (var line in Line2)
{
//actually should define parameter outside the loop and just assign the value here
queryParameters.Clear();
queryParameters.Add(new OleDbParameter("PART_PROGRAM", partProgram));
queryParameters.Add(new OleDbParameter("MACHINE_ID", machineId));
queryParameters.Add(new OleDbParameter("POSITION_ID", Line));
results.Add(this.ExecuteParameterQuery(query, queryParameters));
}
return (results);
}
Line2是一个List,你还在使用它
queryParameters.Add(new OleDbParameter("POSITION_ID", Line2));
试
queryParameters.Add(new OleDbParameter("POSITION_ID", Line));