C#连接多服务器和数据库的方法

How to connect multi server and database in C#

public class MyDatabaseConnection
{
    string connectionString = "Data Source= my DS3;Initial Catalog = MyCATA;Persist Security Info=True;User ID=sa;Password=mypsw*";

    public MyDatabaseConnection(string connectionString)
    {
        this.connectionString = connectionString;
        // create a database connection perhaps
    }

    // some methods for querying a database
    public void execute(string query) { }
}

这是我的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string connectionString ;

    public void searchOnAllDatabases(string query)
    {

        MyDatabaseConnection con1 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER1;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //----1st search here 
        MyDatabaseConnection con2 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER2;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //---- 2nd search here 
        MyDatabaseConnection con3 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER3;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //---- 3rd search here 

        MyDatabaseConnection[] cons = new MyDatabaseConnection[] { con1, con2, con3 };

        foreach (MyDatabaseConnection con in cons)
        {


            MessageBox.Show(Convert.ToString(cons)); //--to see the result only
        }

    }

我的objective是当我搜索这个文件名时,它会从不同的服务器搜索整个数据库。例如,如果找不到,它将在服务器 1 数据库上搜索,如果找到,它将继续到下一个服务器 2 数据库到服务器 3 数据库,它将在 datagridview

中给我结果

您可以像这样更改表单:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


public void searchOnAllDatabases(string query)
{

MyDatabaseConnection con1 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER1;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //----1st search here 
    MyDatabaseConnection con2 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER2;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //---- 2nd search here 
    MyDatabaseConnection con3 = new MyDatabaseConnection("Data Source= 10.232.1.15\SERVER3;Initial Catalog = My Catalog;Persist Security Info=True;User ID=sa;Password=myPSW");  //---- 3rd search here 

MyDatabaseConnection[] cons = new MyDatabaseConnection[] { con1, con2, con3 };

foreach (MyDatabaseConnection con in cons)
{
    var result = con.execute(query);
    if (result)
       break;
}

 }
}

在这里您可以更新您的搜索代码:

public class MyDatabaseConnection
{

string connectionString = "Data Source= my DS3;Initial Catalog = MyCATA;Persist Security Info=True;User ID=sa;Password=mypsw*";

public MyDatabaseConnection(string connectionString)
{
    this.connectionString = connectionString;
    // create a database connection perhaps
}

    // some methods for querying a database
 public bool execute(string query)
    {
        SqlConnection sqlCon = new SqlConnection(connectionString);
        try
        {
            sqlCon.Open();
            SqlDataAdapter sqlDaMonitor = new SqlDataAdapter("select * from TLogging where BatchNumber like '%" + query + "%' ", sqlCon);
            DataTable dtblMonitor = new DataTable();
            sqlDaMonitor.Fill(dtblMonitor);
            if ((dtblMonitor == null) || (dtblMonitor.Rows.Count == 0)) {
                MessageBox.Show("SEARCH OTHER DATABASE");
                myCon.Val += 1;
                MessageBox.Show(myCon.MyDTConn);

                return false;
            }


        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }


}