ASP.Net 连接多个数据库之间的切换

本次两个的两个数据是SQL Server 和ORCAL

首先在Web.congfig中  

<connectionStrings> </connectionStrings>里面添加两个数据连接

在数据库操作类中   写两个构造函数(一个是无参和有参的构造函数) 具体代码如下

    DataAccess() {
            this.ConnectionSettings = ConfigurationManager.ConnectionStrings["FtpFileDownload"];
            //this.ConnectionSettings = StaticValue.ConnectionSettings;
            this.Factory = DbProviderFactories.GetFactory(this.ConnectionSettings.ProviderName);
        }
        public DataAccess(string ConnectionStringName)
        {
            this.ConnectionSettings = ConfigurationManager.ConnectionStrings[ConnectionStringName];
            //this.ConnectionSettings = StaticValue.ConnectionSettings;
            this.Factory = DbProviderFactories.GetFactory(this.ConnectionSettings.ProviderName);
        }

 

然后在调用就可以了 具体代码如下

            string sql1 = "select top 1 * from tableNAme ORDER BY Id DESC";//直接查询数据库的第一一条数据
            int prossed = DataAccess.Instance.ExecuteScalarGetInt(sql1);//直接查询数据库的最后一条数据
            MessageBox.Show(prossed + "         ");

            string BL_NO = "OERT202202I00438";
            DataAccess a= new DataAccess("SHA");
            string selectSQL = "SELECT COUNT(id) FROM tableNAme WHERE BL_NO = '" + id+ "'";
            int prosessed = a.ExecuteScalarGetInt(selectSQL);
             MessageBox.Show(prosessed + "         ");

 

转载于:https://www.cnblogs.com/my1227/p/11213020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值