我是网络编程的初学者,我的oracle数据库连接有问题。我的代码与Mssql一起运行良好,但它不能与oracle一起使用。我想从mssql将db更改为oracle。 我已经下载了odac。这是我的SqlOperations类。
using System;
using System.Collections.Generic;
using System.Data;
using Oracle.DataAccess;
using System.Data.OracleClient;
using System.Data.Odbc;
using System.Linq;
using System.Web;
namespace BagciEmlak
{
public class SqlOperations
{
OracleConnection con;
OracleCommand cmd;
OracleDataAdapter sda;
DataTable dt;
public OracleConnection OracleConnect()
{
try
{
con = new OracleConnection ("Data Source=SYS-CDB12c; User ID=HR; Password=hr; Unicode=true;");
con.Open();
return con;
}
catch (OracleException e)
{
throw;
}
}
public int Command(string ConStr, OracleConnection conn)
{
try
{
cmd = new OracleCommand(ConStr, conn);
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
public DataTable GetDataTable(string ConStr, OracleConnection conn)
{
try
{
dt = new DataTable();
sda = new OracleDataAdapter(ConStr, conn);
sda.Fill(dt);
return dt;
}
catch (Exception e)
{
throw;
}
finally
{
sda.Dispose();
conn.Close();
conn.Dispose();
}
}
public DataRow GetDataRow(string ConStr, OracleConnection conn)
{
dt = GetDataTable(ConStr, conn);
if (dt.Rows.Count==0)
{
return null;
}
else
{
return dt.Rows[0];
}
}
public string GetDataCell(string ConStr, OracleConnection conn)
{
dt = GetDataTable(ConStr, conn);
if (dt.Rows.Count == 0)
{
return null;
}
else
{
return dt.Rows[0][0].ToString();
}
}
}
}
这是我的webconfig代码:
<connectionStrings>
<add name="{ConnectionName}"
connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=HR;Password=HR;"
providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
所以你能帮助我吗?
答案 0 :(得分:1)
只需将OracleConnection中的参数更改为
即可SERVER =(DESCRIPTION =(ADDRESS =(PROTOCOL = TCP)(HOST = SYS-CDB12c)(PORT = 1521))(CONNECT_DATA =(SERVICE_NAME = MyOracleSID))); UID = HR; PWD =小时;
还将myOracleSID更改为您的oracle数据库实例名称。
在您的情况下,web.config中的连接字符串与您的连接无关。因为您使用配置管理器将连接作为字符串参数whitout传递。