I am working in visua开发者_运维技巧l studio with a datagridview that is bound to a mysql database via the .net mysql connector. In my vb app I have some text fields that provide the necessary fields for building the connection string which I have defined as:
Dim connectionString As String = "server=" + sqlHost.Text + ";User Id=" + sqlUser.Text + ";password=" + sqlPassword.Text + ";database=some_database;Persist Security Info=True"
So I was wondering how I can programmatically set the connection string to the string defined in the code above. Also I know storing this information this way is a security threat so anyway ways to secure the connection string would be most helpful. Thanks in advance.
I used the data source configuration wizard to specify the connection string
When you use the wizard Visual Studio makes a whole bunch of choices for you. Assuming you chose dataset the connection string was stored in the app.config connectionstrings section. In the dataset's XSD a Global.YourNameSpace.My.MySettings.Default.YourDataSetConnectionString is set to the appsettings key.
This is why its hard to find it if you don't know where to look. There are a lot of ways to change the connection, but one way is...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MusicDataSet.music_file' table. You can move, or remove it, as needed.
Me.Table1_TableAdapter.Connection = New MySql.Data.MySqlClient.MySqlConnection("server=" + sqlHost.Text + ";User Id=" + sqlUser.Text + ";password=" + sqlPassword.Text + ";database=some_database;Persist Security Info=True" )
Me.Table1TableAdapter.Fill(Me.MyDataSet.Table1)
End Sub
As for securing the strings you can use the built in encryption technology.
Try this - you already have the actual string for the connection string in the code you posted - all you need to do is use it for an actual sql connection
Public Sub ConnectToSql()
Dim conn As New SqlClient.SqlConnection
Dim connectionString As String = "server=" + sqlHost.Text + ";User Id=" + sqlUser.Text + ";password=" + sqlPassword.Text + ";database=some_database;Persist Security Info=True"
conn.ConnectionString = connectionString
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
精彩评论