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 + " ");