aspx连接本地Oracle

1.web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <connectionStrings>
        <add name="调用" connectionString="Data Source='数据库';User Id='用户名';Password='密码';pooling=true" />
        <add name="调用" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.10.10.10)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=数据库)));Persist Security Info=True;User ID=用户;Password=密码;"
	 providerName="Oracle.ManagedDataAccess.Client"/>
    </connectionStrings>
</configuration>

2.工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;//引用data,查询使用
using System.Data.OracleClient;//引用数据库类
using System.Configuration;//所引用config,需要提前引用命名空间

namespace MyTemplate_New2
{
    public class 工具类 
    {		
        //查询
        public DataTable QueryTable(string sql, string strTableName, string strServer)
        {
            string connstr = ConfigurationManager.ConnectionStrings[strServer].ToString();
            OracleConnection conn = new OracleConnection(connstr);

            conn.Open();

            //定义一个数据适配器,适配器可以和增删改查sqlCommand相关联
            OracleDataAdapter mda = new OracleDataAdapter(sql, conn);

            //数据表,用来存储展现数据
            DataSet ds = new DataSet();

            //将查询的数据填充到数据表中
            mda.Fill(ds, strTableName);
					
			//转换为需要的格式
            DataTable dt = ds.Tables[strTableName];

            //各种关闭
            ds.Dispose();
            mda.Dispose();
            conn.Close();
            dt.Dispose();

            //将其返回
            return dt;

        }

    }
}

3.使用

protected void bind()
 {
     工具类 DBAC1 = new 工具类 ();
     String sql = "select * from Test";
     DataTable table = DBAC1.QueryTable(sql, "web2", "调用");
 }

注:需要自己引入System.Data.OracleClient.dll
需要将代码中的中文替换为你自己的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值