最近在讀取ORACLE DATA 時本來用原先OracleClient 方式連Oracle
但卻一直出現
{"嘗試載入 Oracle 用戶端程式庫時傳出 BadImageFormatException。當與具有 32 位元的 Oracle 用戶端元件執行 64 位元模式安裝時,會出現此問題。"}
的錯誤
嘗試了
https://dotblogs.com.tw/chou/2011/10/11/41156
中提到的解法 還是一樣
為了避免日後佈署會遇到同樣的狀況,就放棄OracleClient 改以Oracle.ManagedDataAccess 處理
如何下載與安裝可參考
http://blog.darkthread.net/post-2015-03-31-managed-odp-net.aspx
為了可Reuse 故建立一個Class
並using Oracle.ManagedDataAccess.Client;
#region getOraData
public System.Data.DataTable getOraData(string strConnectionString, string sqlStr, OracleParameter[] parameter)
{
DataTable dt = new DataTable();
OracleConnection conn = new OracleConnection(strConnectionString);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OracleCommand cmd = new OracleCommand(sqlStr, conn);
if (parameter != null)
{
cmd.Parameters.AddRange(parameter);
}
OracleDataAdapter da = new OracleDataAdapter(cmd);
try
{
da.Fill(dt);
}
catch (System.Exception ex)
{
//出錯紀錄,紀錄Log
System.String err;
err = "";
err += "getOraData.Error:" + ex.Message.Trim();
err += System.Environment.NewLine;
err += System.Environment.NewLine;
err += "sqlStr:[" + sqlStr.Trim() + "]";
if (parameter != null)
{
foreach (OracleParameter sqlPara in parameter)
{
if (sqlPara != null)
{
err += System.Environment.NewLine;
err += "[Parameter:" + sqlPara.ParameterName + "]";
err += ";";
err += "[Direction:" + sqlPara.Direction.ToString() + "]";
if (sqlPara.Value != null)
{
err += ";";
err += "[Value:" + sqlPara.Value.ToString() + "]";
}
}
}
}
cls_log objLog = new cls_log();
objLog.WriteLog("getOraData", err, "ERROR");
}
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
da.Dispose();
}
return dt;
}
#endregion getOraData
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在程式中就可以用getOraData來取的DATA了
string conStr = ConfigurationManager.AppSettings["ConStr"].ToString();
DataTable Dt = objsql.getOraData(conStr, strSql, OraPara);
Web.config
<appSettings>
<add key="ConStr" value="DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME = xe)));Persist Security Info=True;User ID=XXX;Password=XXX;"/>
</appSettings>
ref
https://blog.miniasp.com/post/2009/01/22/Using-OracleParameter-will-cause-performance-problem.aspx
留言列表