Two databases communicate with the same model [closed]

1

I wanted to have one database for production and another for development to separate the actual data from the test environment data. The problem is that I have no idea how to do this. I created a clone of my development database with the "Schema Compare" of visual studio. In "debug" mode would run the database development and "release" run the production database. I need ideas!

    
asked by anonymous 27.04.2017 / 12:01

1 answer

1

An alternative answer to the question ConnectionString different for different builds you can use Preprocessing Policies .

" The directives #if, #elif, #else, and #endif are used in conditional preprocessing, for example, to verify that a symbol or symbols are true." line is only compiled according to the configured mode - Release or Debug.

To create a connection string conditioned to different banks (Production and Homologation), use #if (DEBUG) , follow the steps:

Now in your code, you will do the following:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class _Default : Page {
    private string connectionString;
    protected void Page_Load(object sender, EventArgs e) {

#if (DEBUG) //Ao compilar em modo DEBUG, a variável receberá o valor abaixo
        connectionString = "Data Source=seu_servidor;" +
                            "Initial Catalog=seu_banco_de_HOMOLOGACAO;" +
                            "User ID=seu_usuario; " +
                            "Password=sua_senha";
#else
        connectionString = "Data Source=seu_servidor;" +
                            "Initial Catalog=seu_banco_de_PRODUCAO;" +
                            "User ID=seu_usuario; " +
                            "Password=sua_senha";
#endif
    }

    internal void ConexaoBanco() {
        using (SqlConnection conn = new SqlConnection(connectionString)) {
            string query = "SELECT * FROM DBO.TABELA";

            using (SqlCommand cmd = new SqlCommand(query, conn)) {
                DataTable tabelaTeste = new DataTable("tabelaTeste");

                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);

                conn.Open();
                dataAdapter.Fill(tabelaTeste);
                conn.Close();
            }
        }
    }
}

Note: Likewise, this can be done in Windows Forms.

#if (C # Reference)

    
27.04.2017 / 15:13