Question

I am trying to save my connection string to a configuration file but the password is not being submitted.

Here is the code I am using:

    using (var con = new SqlConnection(string.Format("Data Source={0};Initial Catalog={3};User ID={1};Password={2};",
           textBox_SqlServer.Text, textBox_Username.Text, textBox_Password.Text, comboBox_DatabaseName.Text)))
    {
      // test connection before continuing
      con.Open();
      configs.ApplicationConfiguration.SetConnectionString(con.ConnectionString);
    }

All is fine until con.ConnectionString and it does not include the password. What do I need to do to So, naturally, when I go to retrieve it, the password is not there.

How do I get the password to set also?

Was it helpful?

Solution

The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

From the MSDN documentation. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top