Two databases communicate with the same template

Asked

Viewed 42 times

1

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

  • 1
  • Hello Simon, consider accepting my answer if it has been useful to you. If you think she’s incomplete or doesn’t respond to you, make the appropriate comments so I can improve her.

1 answer

1


An alternative answer in the question Different connectionstring for different builds you can use the Directives for pre-processing.

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

To create a connection string conditioned to different banks (Production and Approval), use the #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 (reference of C#)

Browser other questions tagged

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