自己写的一个.net跨服务器查询方法

首先在webconfig中加入链接

 <connectionStrings>
    <add name="ConnectionString" connectionString="Server=10.0.0.188;database=HA_Psy_Test;User ID=sa;Password=!qwe123456"/>
    <add name="ConnectionString1" connectionString="Server=10.0.0.188;database=ZD_EPDB_WEB_Huai;User ID=sa;Password=!qwe123456"/>
  </connectionStrings>

接下来写一个存放表名的model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PsyModel.System
{
    [Serializable]
    public partial class SuperTableInfo
    {
        private int _server;
        private string _fullName;
        private string _alias;
        /// <summary>
        /// 服务器
        /// </summary>
        public int Server
        {
            set { _server = value; }
            get { return _server; }
        }
        /// <summary>
        /// 表全称:例如([DataBase].[dbo].[Table])
        /// </summary>
        public string FullName
        {
            set { _fullName = value; }
            get { return _fullName; }
        }
        /// <summary>
        /// 表别名
        /// </summary>
        public string Alias
        {
            set { _alias = value; }
            get { return _alias; }
        }

    }
}

接着是查询方法

using PsyModel.System;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace PsyDAL
{
     public abstract class AcrossServersQuery
    {
         //protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
         //protected static string connectionString1 = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
        protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        protected static string connectionString1 = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
         public static DataSet Query(string selectItems,List<SuperTableInfo> list,string strWhere)
         {
             string[] linkOne = connectionString.Split(';');
             string[] linkTwo = connectionString1.Split(';');
             char equal = '=';

             string IPOne = linkOne[0].Substring(linkOne[0].IndexOf(equal) + 1);
             string UIDOne = linkOne[2].Substring(linkOne[2].IndexOf(equal) + 1);
             string PWOne = linkOne[3].Substring(linkOne[3].IndexOf(equal) + 1);
             string IPTwo = linkTwo[0].Substring(linkTwo[0].IndexOf(equal) + 1);
             string UIDTwo = linkTwo[2].Substring(linkTwo[2].IndexOf(equal) + 1);
             string PWTwo = linkTwo[3].Substring(linkTwo[3].IndexOf(equal) + 1);
             //开启数据库跨服务器查询功能
             StringBuilder sbOpen = new StringBuilder();
             sbOpen.Append(" exec sp_configure 'show advanced options',1 ");
             sbOpen.Append(" reconfigure ");
             sbOpen.Append(" exec sp_configure 'Ad Hoc Distributed Queries',1 ");
             sbOpen.Append(" reconfigure ");
             DbHelperSQL.Query(sbOpen.ToString());
             //查询语句
             StringBuilder sb = new StringBuilder();
             sb.Append(selectItems);
             StringBuilder allItem = new StringBuilder();
             foreach (SuperTableInfo item in list)
             {
              
                 StringBuilder sbItem = new StringBuilder();
                 sbItem.Append(" OPENROWSET('SQLOLEDB','");
                 if (item.Server == 1)
                 {
                     sbItem.Append(IPOne);
                     sbItem.Append("';'");
                     sbItem.Append(UIDOne);
                     sbItem.Append("';'");
                     sbItem.Append(PWOne);
                     sbItem.Append("', ");
                 }
                 else if (item.Server == 2)
                 {
                      sbItem.Append(IPTwo);
                      sbItem.Append("';'");
                      sbItem.Append(UIDTwo);
                      sbItem.Append("';'");
                      sbItem.Append(PWTwo);
                      sbItem.Append("', ");
                 }
                 sbItem.Append(item.FullName);
                 sbItem.Append(" ) ");
                 sbItem.Append(item.Alias);
                 sbItem.Append(" ,");

                 allItem.Append(sbItem);
             }
             sb.Append(allItem.ToString().TrimEnd(','));
             sb.Append( strWhere );

           DataSet ds =  DbHelperSQL.Query(sb.ToString());
             //关闭数据库跨服务器查询功能
             StringBuilder sbClose = new StringBuilder();       
             sbClose.Append("  exec sp_configure 'Ad Hoc Distributed Queries',0 ");
             sbClose.Append(" reconfigure ");
             sbClose.Append(" exec sp_configure 'show advanced options',0 ");
             sbClose.Append(" reconfigure ");
             DbHelperSQL.Query(sbClose.ToString());

             return ds;
         }
    }
}
最后  在后台调用这个方法

DataSet ds3 = new DataSet();
                List<SuperTableInfo> list = new List<SuperTableInfo>();          
                SuperTableInfo PsyNewsCommnet = new SuperTableInfo();
                PsyNewsCommnet.Server = 1;
                PsyNewsCommnet.FullName = "[HA_Psy_Test].[dbo].[PsyNewsCommnet]";
                PsyNewsCommnet.Alias = "a";
                list.Add(PsyNewsCommnet);
                SuperTableInfo Pu_User = new SuperTableInfo();
                Pu_User.Server = 2;
                Pu_User.FullName = "[ZD_EPDB_WEB_Huai].[dbo].[Pu_User]";
                Pu_User.Alias = "b";
                list.Add(Pu_User);
                SuperTableInfo Psy_User = new SuperTableInfo();
                Psy_User.Server = 2;
                Psy_User.FullName = "[ZD_EPDB_WEB_Huai].[dbo].[Psy_User]";
                Psy_User.Alias = "c";
                list.Add(Psy_User);
                string selectItems = " select c.imgurl,a.username,a.[content],a.applydate from ";
                string strWhere = " where b.UserID = c.UserID and a.username=b.username and a.Newsid=" + Newsid + " order by a.id desc  ";
                ds3 = AcrossServersQuery.Query(selectItems,list,strWhere);

OK,结束。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值