1

I'm trying to connect to a SQL Server database that is not local. I have the Data Source and Initial Catalog - no issues. But need to change Integrated Security to False and insert SQL Server credentials.

Does anyone have any idea how put that in the connection string?

Also, does anyone know how to handle SecureStrings?

Here is my code so far:

Dim pwd As New SecureString("Password")
Dim cred As New SqlCredential("Username", pwd)
Dim sql As New SqlConnection("Data Source=OnlineServer;Initial Catalog=DatabaseName;Integrated Security=False")

2 Answers 2

3

Have a look at here: SQL Connection Strings to hopefully find which one you need. This will give you the basics.

To make the SQL account credentials confidential, you should encrypt the <connection strings> section in the web.config. to do so:

aspnet_regiis -pe "connectionStrings" -app "OnlineServer" -prov "DataProtectionConfigurationProvider"

Retrieving your connection string using ConfigurationManager will automatically decrypt the string

Dim connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

Here is a Microsoft Link that explains it further.

0
0

I worked out what I needed to do and how to handle secure strings.

Here is a code snippet for anyone who struggles in the future:

Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Security 
Public Module secure
    Public Function sql()
        Dim pass As String = "Password"
        Dim pwd As SecureString = New SecureString()
        For Each ch As Char In pass
            pwd.AppendChar(ch)
        Next
        pwd.MakeReadOnly()
        Dim cred As New SqlCredential("SQL_Login", pwd)
        Dim conn As New SqlConnection("Server=Database_Name;Initial Catalog=Database_Address;Integrated Security=False", cred)
        Return conn
    End Function
End Module

Public Class sqlCommunications
    Dim sql As New SqlConnection
    Dim sqlcom As New SqlCommand
    Public Sub start()
        sql = secure.sql
        sqlcom.Connection = sql
        sql.Open()
        sql.Close()
    End Sub
End Class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.