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)