When choosing a company connect to the Database

Asked

Viewed 402 times

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 I should work example Company A, Company B, Company c, in sql server I have 3 bdEmpresaA banks, bdEmpresaB, bdEmpresaC, when selecting the company wanted to connect to the corresponding bank more I have no idea how to do this follows the screens below

Tela Login

inserir a descrição da imagem aqui

String Connection screen

inserir a descrição da imagem aqui

Business register

inserir a descrição da imagem aqui

my bank

 SqlConnection CON = new SqlConnection(); //Faz a conexão com o Banco de Dados
            CON.ConnectionString = Properties.Settings.Default.csSistemaEstoque; // Cria uma string de conexão
            SqlCommand CM = new SqlCommand(); //
            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, I’d appreciate it

login screen code

        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" />
  • Show the selection code and how you would do for a company.

  • I don’t understand your question

1 answer

2


You’ll have to change your ConnString at 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;
        }
    }
}
  • didn’t work out look my code put on the login screen updated my question

  • what went wrong?

  • it always connects to the same database. in question this to conection string

  • So the way you want it, you’ll have to take the connection from the config app and use the one you’re created in your method cbbEmpresaUsuaria_SelectedIndexChanged

  • I’m new at this you could give me an example please,

  • I changed the answer

  • Marco cracked the cuca more seems to work, let me ask you my project is 4 layers when I call the frmMenu step as parameter to Connection and on all screens I open this Connection to the DAO layer and there I do inclusion, query and change, That’s how it’s done, right. Hug

  • Ideally you create Gets and Sets properties for your connection and create a Base Class something like this FormBase : Form and instead of you inheriting the class Form you would inherit your class FormBase you would already have your connection. See the example in the answer.

  • 1

    understood could create a class with the connections and when choosing the use connection it in the application with the chosen company, I thank you very much for the help. hug

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.