Connection to Database (SQL SERVER) Demo VB.NET - TOP10

Connection to Database (SQL SERVER) Demo VB.NET

Now We Show How to create Connection to Database(SQL SERVER) use (Imports System.Data.SqlClient) from VB.NET Code.For beginner programmer or student to start learning
Programming can practise in this example it is very simple way to learn.


VB.NET Connect to Database DEMO

**********************************Code Start*************************************

Imports System.Data.SqlClient

Public Class FrmLogon

 Dim SqlCon As SqlConnection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
 Try
  If txtServer.Text = "" Then MsgBox("Please enter server name", MsgBoxStyle.Exclamation)
      txtServer.Select()
      Exit Sub
   End If
   If txtDB.Text = "" Then MsgBox("Please enter database name", MsgBoxStyle.Exclamation)                  txtDB.Select()
     Exit Sub
  End If

 If cboAuthentication.Text = "" Then              
   MsgBox("Please Select Authentication type",  MsgBoxStyle.Exclamation)                                            cboAuthentication.Select()              
   Exit Sub          
 End If

 If cboAuthentication.Text = "SQL Server Authentication" Then              
   SqlCon = New SqlConnection("Server='" & txtServer.Text & "';Database='" & txtDB.Text &              "';UID='" & txtUser.Text & "';pwd='" & txtPassword.Text & "'")
   SqlCon.Open()          
ElseIf cboAuthentication.Text = "Windows Authentication" Then
   SqlCon = New SqlConnection("Server='" & txtServer.Text & "';Database='" & txtDB.Text &              "';Integrated Security=True")              
   SqlCon.Open()          
End If
   Hide()
   MainForm.Show()      
Catch ex As Exception          
  MsgBox("Connection error", MsgBoxStyle.Critical)      
End Try  
End Sub

Private Sub cboAuthentication_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAuthentication.SelectedIndexChanged      
 If cboAuthentication.Text = "Windows Authentication" Then          
   txtUser.Clear()          
   txtPassword.Clear()          
   txtUser.Enabled = False          
   txtPassword.Enabled = False      
  Else          
   txtUser.Enabled = True          
   txtPassword.Enabled = True      
  End If  
 End Sub

End Class

*************************Code stop**********************************************


Previous
Next Post »