When choosing a company to connect to the Database

1
Aguem could help me I'm doing a project to deliver this year in college in 4 layers I have a login screen and when I type user and password I choose the company that I should work example Company A, Company B, Company c, in sql server I have 3 banks bdCompanyA, bdCompanyB, bdCompanyC, when selecting the company wanted to connect to the corresponding bank more I have no idea how to do this follows the screens below

Login screen

Connectionconnectionscreen

CompanyRegistration

Mybank

SqlConnectionCON=newSqlConnection();//FazaconexãocomoBancodeDadosCON.ConnectionString=Properties.Settings.Default.csSistemaEstoque;//CriaumastringdeconexãoSqlCommandCM=newSqlCommand();//CM.CommandType=System.Data.CommandType.Text;CON.Open();CM.CommandText="INSERT INTO tbEmpresaUsuaria ([dtInclusao_emp], [razao_emp], [nomeFant_emp], [cnpj_emp], [inscEst_emp], [logradouro_emp], [endereco_emp], " +
            " [numero_emp], [compl_emp], [bairro_emp], [cidade_emp], [uf_emp], [cep_emp], [tel_emp], [tel2_emp], [email_emp], [site_emp], [endLogo_emp])" +
            " VALUES (@dtInclusao_emp, @razao_emp, @nomeFant_emp, @cnpj_emp, @inscEst_emp, @logradouro_emp, @endereco_emp, @numero_emp, " +
            " @compl_emp, @bairro_emp, @cidade_emp, @uf_emp, @cep_emp, @tel_emp, @tel2_emp, @email_emp, @site_emp, @endLogo_emp)";

If anyone can help me thank you

code on login screen

        private void cbbEmpresaUsuaria_SelectedIndexChanged(object sender, EventArgs e)
    {
       string myConnString = String.Empty;

        codEmpUsuaria = Convert.ToString(cbbEmpresaUsuaria.SelectedValue);// pego o codigo empresa usuaria selecionado no ComboBox


          var empresa = codEmpUsuaria;

          if (empresa.Equals("1"))
          {
            myConnString = "Data Source=servidor;Initial Catalog=empresaA;User ID=teste;Password=123456";

          } if (empresa.Equals("2"))
          {
                    myConnString = "Data Source=servidor;Initial Catalog=empresaB;User ID=teste;Password=123456";

          }



    }




<connectionStrings>
    <add name="Data Source=servidor;Initial Catalog=dbSistEstoqueEmp;User ID=teste;Password=123456"
        providerName="System.Data.SqlClient" />
    
asked by anonymous 08.01.2016 / 00:13

1 answer

2

You will have to change your ConnString in the event SelectedIndexChanged of your comboBox . This can be done as follows.

   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;
        }
    }
}
    
08.01.2016 / 13:20