Skip to main content
3 de 3
adicionou 288 caracteres ao conteúdo
Marconcilio Souza
  • 12,6mil
  • 10
  • 40
  • 106

Você vai ter que mudar sua ConnString no evento SelectedIndexChanged do seu comboBox. Isso pode ser feito da seguinte forma.

   using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var comboBox = comboBox1.SelectedItem;
            string myConnString = String.Empty;
            if (comboBox.Equals("Empresa A"))
            {
                myConnString = "Server=.\\SQLEXPRESS;Database=NORTHWND;User ID=sa;Password=*****";
            }
            if (comboBox.Equals("Empresa B"))
            {
                myConnString = "Server=.\\SQLEXPRESS;Database=bdEmpresaB;User ID=******;Password=******";
            }
            if (comboBox.Equals("Empresa C"))
            {
                myConnString = "Server=.\\SQLEXPRESS;Database=bdEmpresaC;User ID=******;Password=******";
            }

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = myConnString;
            try
            {
                var SQL = string.Format("SELECT * FROM Categories");

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL;
                SqlDataAdapter sqlA = new SqlDataAdapter();
                DataTable tabela = new DataTable();

                sqlA.SelectCommand = cmd;

                conn.Open();
                sqlA.Fill(tabela);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}



public partial class FormBase : Form
{
    public string myConnString
    {
        get
        {
            return "Server=.\\SQLEXPRESS;Database=BancoModelo;User ID=sa;Password=........";
        }
        set
        {
            myConnString = value;
        }
    }
}
Marconcilio Souza
  • 12,6mil
  • 10
  • 40
  • 106