
Originally Posted by
jmcilhinney
Just as you're creating a SqlCommand object and configuring it, so you create a SqlConnection object and configure it and then assign it to the Connection property of the command.
I have my connection object in a module listed below.... how do i assign it to the connection property of the command?
Code:
Public Function VerifyConnection() As Boolean
VerifyConnection = False
conn = New SqlClient.SqlConnection
connstrg = "Persist Security Info=False;User ID=" + user + ";Password=" + pwd + ";Initial Catalog=" + AppDB + ";Server=" + Servername
conn.ConnectionString = connstrg
Try
conn.Open()
If conn.State = ConnectionState.Open Then
VerifyConnection = True
End If
conn.Close()
Catch ex As Exception
VerifyConnection = False
MessageBox.Show(ex.Message)
End Try
End Function
Code:
Public Sub OpenSQLConnection() If conn Is Nothing Then
Try
conn = New SqlClient.SqlConnection
Catch ex As Exception
End Try
End If
If conn.State = ConnectionState.Open Then
Exit Sub
End If
Try
conn.Open()
Catch ex As Exception
Console.WriteLine("ERROR: An error occurred while trying to connect to the database. " + vbCrLf + _
"Here is the error message returned from the system: " + vbCrLf + ex.Message)
End Try
End Sub
Bookmarks