How to change the connection string (server ip, user and password) of a MySql data source in Visual Basic 2010?
Friday, June 01, 2012 9:07 PM
Hi, I have a project where I added visually and successfully a datasource of MySql. I binded a datagridview with a table.
But, how do I change programatically the password, ip or user to connect with the MySql server? Because it can change at any time, so I can let the user change these values.
Thanks in advance.
Sunday, June 03, 2012 8:40 AM
Be aware this is a Microsoft forum in which are databases like SQL Server, SQL Server CE, SQL Server Express, Jet 4, Jet 12, beside the first one all freeware ones.
So MySQL is rarely used, better to do your question in a MySQL forum.
Sunday, June 03, 2012 4:34 PMOk, I will do it. Anyway, my question is general, it would apply to a datasource of Sql Server or Access, how do I change programatically the settings of data source visually configured? Thanks.
Sunday, June 03, 2012 4:43 PM
This is the code I use:
conn = NewMySql.Data.MySqlClient.MySqlConnection
conn.ConnectionString = "Server=127.0.0.1; Database=data; User=username; Password=******"
Hope this will help you.
Sunday, June 03, 2012 5:36 PM
Hi Rafa, thanks for your answer. I use that code to establish a manual connection, but what I want is to modify the connection set up visually with the datasource.
Hola Rafa, gracias por tu respuesta. Ese código yo también lo uso pero para establecer la conexión manualmente, lo que quiero es modificar la que hice con el datasource.
Sunday, June 03, 2012 6:39 PM
Ok, I will do it. Anyway, my question is general, it would apply to a datasource of Sql Server or Access, how do I change programatically the settings of data source visually configured? Thanks.
Depends if you use a strongly typed dataset (made by Microsoft for OleDB and SQLClient and in fact independent from the rest or simply a raw one, in the later case simply setting like Rafael shows otherwise, you know probably what I want to write.
Monday, June 04, 2012 2:51 PM
The connection strings are stored in the .config file of the application. See the below link on how to change at run time:
Paul ~~~~ Microsoft MVP (Visual Basic)
- Marked As Answer by Denis Pujadas Tuesday, June 05, 2012 1:42 PM
Tuesday, June 05, 2012 1:47 PM
Thanks, you are very kind! After 20 hours of investigation, I could do it only with this little piece of code which I found in the link you gave me. I almost have decided to change the app.config file, but the final solution is far easier. I put this in the settings.vb class:
Namespace My 'This class allows you to handle specific events on the settings class: ' The SettingChanging event is raised before a setting's value is changed. ' The PropertyChanged event is raised after a setting's value is changed. ' The SettingsLoaded event is raised after the setting values are loaded. ' The SettingsSaving event is raised before the setting values are saved. Partial Friend NotInheritable Class MySettings Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
'here I put whatever I want as connection string, being balancemysql the name of the property which contains the connection string
Me.Item("balancemysql") = Conexion.cambiarcadena End Sub End Class
- Marked As Answer by Denis Pujadas Tuesday, June 05, 2012 2:17 PM