我的任务是将我们的一些C#代码转换为使用Oracle而不是SQL Server。我对甲骨文的经历是......哦...... 2天。
所以,这就是我所拥有的:
private void LoadPreferences()
{ // inital load of users function and role based on last action performed in database
try
{
OracleConnection oConn = GetConnection();
//string selectSQL = "select Function_ID,Role_ID from vw_mos_DPL_Last_Selection where Lan_ID = '" + strUserID + "'";
//string selectSQL = "select Function_ID,Role_ID from vw_mos_DPL_Last_Selection where Lan_ID = @strUserID";
//string selectSQL = "SELECT WORK_ID, ROLE_ID, ACTIVITY_ID, LAN_ID, CREATE_TS FROM THN_DPL_DETAIL WHERE Lan_ID = @strUserID ORDER BY CREATE_TS DESC";
string selectSQL = "SELECT * FROM(";
selectSQL = selectSQL + "SELECT TB.BUS_ID, TL.LOB_ID, TD.ROLE_ID, TL.LOB_ID, TD.ACTIVITY_ID, TD.LAN_ID, TD.CREATE_TS ";
selectSQL = selectSQL + "FROM THN_DPL_DETAIL TD ";
selectSQL = selectSQL + "LEFT JOIN THN_ACTIVITY TA ON TD.Activity_ID = TA.Activity_ID ";
selectSQL = selectSQL + "LEFT JOIN THN_ROLE TR ON TR.ROLE_ID = TA.Role_ID ";
selectSQL = selectSQL + "LEFT JOIN THN_LOB TL On TL.LOB_ID = TA.LOB_ID ";
selectSQL = selectSQL + "LEFT JOIN THN_BUSINESS TB ON TB.BUS_ID = TR.BUS_ID ";
selectSQL = selectSQL + "WHERE Lan_ID = @strUserID ";
selectSQL = selectSQL + "ORDER BY CREATE_TS DESC";
selectSQL = selectSQL + ") WHERE ROWNUM = 1;";
OracleCommand cmd = new OracleCommand(@selectSQL, oConn);
cmd.Parameters.AddWithValue("@strUserID", strUserID);
OracleDataReader reader;
oConn.Open();
reader = cmd.ExecuteReader();
//read first line
reader.Read();
if (reader.HasRows == true)
{
//First, grab the bytes from the reader: AA = Bus_ID, BB = LOB_ID and CC = Role_ID
byte AA = reader.GetByte(0);
byte BB = reader.GetByte(1);
byte CC = reader.GetByte(2);
//Now set the SelectedValue of Business and re-query the LOB dropdown for eligible values
CallBus_DrpDwnLst.SelectedValue = AA.ToString();
LOBLoad();
//Now set the SelectedValue of LOB since it's filled with eligible valuse
CallLOB_DrpDwnLst.SelectedValue = BB.ToString();
CallRoleLoad();
//Now set the SelectedValue of Role since it's filled with eligible valuse
CallRole_DrpDwnLst.SelectedValue = CC.ToString();
}
else //no rows found, clear selection
{
CallBus_DrpDwnLst.SelectedIndex = 0;
CallLOB_DrpDwnLst.SelectedIndex = 0;
CallRole_DrpDwnLst.SelectedIndex = 0;
}
//close the reader
reader.Close();
oConn.Close();
}
catch (Exception ex)
{
// Handle the error
Response.Write(ex.Message);
//ErrorLogging.WriteToEventLog(ex);
}
}
我遇到的问题是reader = cmd.ExecuteReader()
我遇到了错误:
ORA-01036:非法变量名称/编号
任何人都可以帮我解决这个问题吗?我知道strUserID正确解析,因为我在单步执行代码时检查了它。
答案 0 :(得分:1)
错误是因为@
占位符中的@strUserID
。 Oracle使用冒号表示绑定变量,addWithValue()
调用应使用纯绑定名称。
分号也是一个语句分隔符,因此它不构成单个语句的一部分;这有时会导致一个ORA-00911,但是在这里你的驱动程序似乎在Oracle可以抛出该错误之前给你'SQL命令没有正确结束'。
所以你的代码应该是:
...
selectSQL = selectSQL + "WHERE Lan_ID = :strUserID ";
selectSQL = selectSQL + "ORDER BY CREATE_TS DESC";
selectSQL = selectSQL + ") WHERE ROWNUM = 1";
OracleCommand cmd = new OracleCommand(@selectSQL, oConn);
cmd.Parameters.AddWithValue("strUserID", strUserID);
...
答案 1 :(得分:0)
而不是执行select *给出返回值名称以匹配您的C#过程期望的内容,如
select Function_ID,Role_ID from ( your inner select)