Hi,
try this demo:
Imports System.Data.SqlClient
Public Class Form28
Private dgv As New DataGridView With {.Dock = DockStyle.Fill}
Private Sub Form28_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.Add(dgv)
' How to use Module '
Using da As New SqlDataAdapter("SELECT * FROM Tab1", DataBase.Connection())
Dim dt As New DataTable
da.Fill(dt)
dgv.DataSource = dt.DefaultView
End Using
End Sub
End Class
''' <summary>
''' Module to open SQL Server Connection
''' </summary> '
Public Module DataBase
Private Property mysqldbo As New SqlParams
Private cn As SqlConnection
Public Function Connection(Optional newconnection As Boolean? = Nothing) As SqlConnection
If cn Is Nothing OrElse (newconnection.HasValue And newconnection.Value) Then
cn?.Close
cn?.Dispose
mysqldbo.ShowDialog()
Dim connection_string = $"server={mysqldbo.server};user id={mysqldbo.username};password={mysqldbo.password};database={mysqldbo.databasename}"
cn = New SqlConnection(connection_string)
cn.Open()
End If
Return cn
End Function
Public Class SqlParams
Inherits Form
Public Property databasename As String
Public Property server As String
Public Property username As String
Public Property password As String
Private lbldbo As New Label With {.Text = "Server Name", .Top = 10, .Left = 10, .Width = 100}
Public txtdbo As New TextBox With {.Top = 10, .Left = 120, .Width = 200}
Private lblserver As New Label With {.Text = "Database Name", .Top = 40, .Left = 10}
Public txtserver As New TextBox With {.Top = 40, .Left = 120, .Width = 200}
Private lbluser As New Label With {.Text = "UserName", .Top = 70, .Left = 10}
Public txtuser As New TextBox With {.Top = 70, .Left = 120, .Width = 200}
Private lblpass As New Label With {.Text = "Password", .Top = 100, .Left = 10}
Public txtpass As New TextBox With {.Top = 100, .Left = 120, .Width = 200}
Private WithEvents btn As New Button With {.Text = "Save", .Top = 130, .Left = 200, .Width = 100}
Private Sub SqlParams_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = 350
Me.Controls.AddRange(New Control() {lbldbo, txtdbo, lblserver, txtserver, lbluser, txtuser, lblpass, txtpass, btn})
txtdbo.DataBindings.Add("Text", Me, "databasename")
txtserver.DataBindings.Add("Text", Me, "server")
txtuser.DataBindings.Add("Text", Me, "username")
txtpass.DataBindings.Add("Text", Me, "password")
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Me.Close()
End Sub
End Class
End Module