ASP.NET与Oracle DB的连接

时间:2015-11-30 22:09:34

标签: asp.net oracle11g

我不明白为什么会出现此错误消息。没有代码用于连接,只有SQLDatasource和网格视图。我正在使用此代码:

protected void Page_Load(object sender,EventArgs e)

{
     try
    {
        using(OracleConnection conn = new OracleConnection("....."))
        using(OracleCommand cmd = new OracleCommand("select * from t1", conn))
        {
            conn.Open();
            using(OracleDataReader reader = cmd.ExecuteReader())
            {
                 DataTable dataTable = new DataTable();
                 dataTable.Load(reader);
                 ListBox1.DataSource = dataTable;
            }
        }
    }
    catch (Exception ex)
    {
        Label1.Text=ex.Message;
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

  1. 首先,您已安装oracle数据访问客户端,然后尝试此操作 以下代码

    • protected void Button1_Click(object sender,EventArgs e)        {            string connectionString ="数据源= DESCRIPTION =" +    "(ADDRESS =(PROTOCOL = TCP)(HOST = ho

      • 列出项目 st_name)(PORT = 1521))" + "(CONNECT_DATA =" + " (SERVER = DEDICATED)" + " (SERVICE_NAME = your_service_name)" + ")" +");用户ID = ID;密码=密码;&#34 ;;        Oracle.DataAccess.Client.OracleConnection con = new Oracle.DataAccess.Client.OracleConnection();        con.ConnectionString = connectionString;        con.Open();        Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand();        cmd.CommandText ="从money_trn中选择ref_no,其中ref_no = 20170733&#34 ;;        cmd.Connection = con;        con.Close();        cmd.CommandType = System.Data.CommandType.Text;        Oracle.DataAccess.Client.OracleDataReader dr = cmd.ExecuteReader();        dr.Read();        TextBox1.Text = dr.GetString(0);    }

答案 1 :(得分:-1)

您无法为Oracle使用 SqlConnection ! 首先,您必须将 connectionString 添加到 web.config 以连接到Oracle。 其次,将引用 System.Data.OracleClient 添加到项目中。 然后,将 SqlConnection 替换为 OracleConnection 。一切,你使用 Sql 的东西,你必须用 Oracle 替换。