I am connecting to a database with SQLDataClient in C#. I am using the following method:
public static string stringConection = "";
public async Task<List<object>> SelectFromDatabase(Conexion con,string consulta)
{
List<object> resultados = new List<object>();
try
{
bool hayConsulta = consulta != null ? true : false;
if (hayConsulta)
{
bool servidor = !string.IsNullOrWhiteSpace(con.Servidor) ? true : false;
bool bd = !string.IsNullOrWhiteSpace(con.BD) ? true : false;
bool usuario = !string.IsNullOrWhiteSpace(con.Usuario) ? true : false;
bool clave = !string.IsNullOrWhiteSpace(con.Password) ? true : false;
if (servidor && bd && usuario && clave )
{
stringConection= "Server=" + con.Servidor + ";Initial Catalog=" + con.BD + ";User Id=" + con.Usuario + ";Password=" + con.Password + ";";
using (var conexion=new SqlConnection())
{
conexion.ConnectionString = stringConection;
await conexion.OpenAsync();
using (var comando=new SqlCommand(consulta, conexion))
{
await comando.ExecuteNonQueryAsync();
using (var reader=await comando.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
foreach (var item in reader.Cast<DbDataRecord>())
{
for (int i = 0; i < item.FieldCount; i++)
{
if (item.GetValue(i) != null)
{
var anonimo = new { columna = item.GetName(i), valor = item.GetValue(i) };
resultados.Add(anonimo);
}
}
}
}
}
}
}
return resultados;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch (Exception e)
{
return null;
}
}
However, something very strange happens. When I pass the stringConnection to the ConnectionString of the SQLDataConnection instance, it eliminates the Password of the connectionString and the SQLCommand does not bring me any record.
As you can see here you have all the correct string:
But after OpenAsync() and before executing the query, the string changes:
As you can see I cut the Password !!!!. Any suggestion?
The disappearance of the password is by design, basically a security measure. In the documentation for SqlConnection.ConnectionString it is specified: