我有这个问题很长时间而且无法解决>希望我能在这里得到解决方案。我已经安装了11g的Oracle Express版本,并且我已经创建了表空间和模式。然后,我已经安装了ASP.NET。 当我编译项目时,它显示conn.open()的错误。
我试过这个连接
private void clidrop()
{
string oradb = "DATA SOURCE=xe;USER ID=AMC; password=amc;";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
string smst = "select *FROM TECHNOVA_SMS_CLNT_MTR";
OracleCommand cmdt = new OracleCommand(smst, conn);
cmdt.CommandType = CommandType.Text;
DataSet dst = new DataSet();
OracleDataAdapter dtt = new OracleDataAdapter(cmdt);
dtt.Fill(dst);
clientName.DataSource = dst.Tables[0];
clientName.DataTextField = "SMS_CLNT_NAME";
clientName.DataValueField = "SMS_CLNT_ID";
clientName.DataBind();
clientName.Items.Insert(0, " --Select--");
clientName.Items[0].Selected = true;
clientName.Items[0].Attributes["disabled"] = "Disabled";
conn.Close();
conn.Dispose();
}
错误是:
执行当前Web请求期间发生了未处理的异常
答案 0 :(得分:0)
尝试改变:
string oradb = "DATA SOURCE=//localhost:1521/XE;User Id=AMC; password=amc;";
仅使用XE,驱动程序假定已在tnsnames.ora文件中声明了名为XE的服务。
答案 1 :(得分:0)
尝试此代码示例(但请注意,不推荐使用System.Data.OracleClient):
using System.Data.OracleClient;
using System.Data;
class Example
{
private string m_sConnectionString;
private OracleConnection m_dbConnection = null;
public OracleConnection Connection
{
get
{
if (null == m_dbConnection)
{
m_dbConnection = new OracleConnection(m_sConnectionString);
}
return m_dbConnection;
}
}
/// <summary>
/// Open the connection
/// </summary>
public void Open()
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
}
/// <summary>
/// Close the connection
/// </summary>
public void Close()
{
try
{
if (null == m_dbConnection)
return;
m_dbConnection.Close();
}
catch { }
}
/// <summary>
/// Execucte a SQL command, with open a new connection
/// </summary>
/// <param name="query">The SQL Query</param>
public void Execute(string query)
{
Open();
using (OracleCommand dbSqlCmd = Connection.CreateCommand())
{
dbSqlCmd.CommandText = query;
dbSqlCmd.ExecuteNonQuery();
dbSqlCmd.Dispose();
}
}
public void Connect(string schema, string password, string datasource)
{
OracleConnectionStringBuilder conn_builder = new OracleConnectionStringBuilder();
conn_builder.DataSource = datasource.Replace("/", @"\"); //Replace the / with a \ (standard path);
conn_builder.Add("Password", password);
conn_builder.Add("User ID", schema);
conn_builder.Pooling = true;
conn_builder.MaxPoolSize = 40;
conn_builder.MinPoolSize = 40;
conn_builder.Unicode = true;
m_sConnectionString = conn_builder.ConnectionString;
}
}