Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

SQL Connection Test

Posted on December 3, 2013October 26, 2015 By David Kittell
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	Dim SqlConn As New SqlConnection
	Dim SqlConnStr As String = "Server=<Server Name/IP>;Database=<Database Name>;Integrated Security=True"

	If SqlConn.State = ConnectionState.Closed Then
		SqlConn.ConnectionString = SqlConnStr
		Try
			SqlConn.Open()
			 MessageBox.Show("Succsessfull DB Connnection", "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show("Invalid DB SqlConnnection" + vbCrLf + Err.Description, "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try
	End If
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			SqlConnection SqlConn = new SqlConnection();
			string SqlConnStr = "Server=<Server Name/IP>;Database=<Database Name>;Integrated Security=True";

			if (SqlConn.State == ConnectionState.Closed)
			{
				SqlConn.ConnectionString = SqlConnStr;
				try
				{
					SqlConn.Open();
					MessageBox.Show("Succsessfull DB Connnection", "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Invalid DB SqlConnnection" + Environment.NewLine + ex.Message, "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
	}
}
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	Dim SqlConn As New SqlConnection
	Dim SqlConnStr As String = "Data Source=<Server Name/IP>;Database=<Database Name>;Persist Security Info=True;User ID=<Username>;Password=<Password>"

	If SqlConn.State = ConnectionState.Closed Then
		SqlConn.ConnectionString = SqlConnStr
		Try
			SqlConn.Open()
			 MessageBox.Show("Succsessfull DB Connnection", "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show("Invalid DB SqlConnnection" + vbCrLf + Err.Description, "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try
	End If
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			SqlConnection SqlConn = new SqlConnection();
			string SqlConnStr = "Data Source=<Server Name/IP>;Database=<Database Name>;Persist Security Info=True;User ID=<Username>;Password=<Password>";

			if (SqlConn.State == ConnectionState.Closed)
			{
				SqlConn.ConnectionString = SqlConnStr;
				try
				{
					SqlConn.Open();
					MessageBox.Show("Succsessfull DB Connnection", "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Invalid DB SqlConnnection" + Environment.NewLine + ex.Message, "DB Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
	}
}

For more connection string assistance visit: http://www.connectionstrings.com/

Originally Posted on December 3, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code CSharp MSSQL SQL VB.NET

Post navigation

Previous post
Next post

Related Posts

MSSQL Get Domain From Email Address

Posted on October 19, 2018

SELECT DISTINCT RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) Domain FROM contacts SELECT RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) Domain, COUNT(Email) EmailCount FROM contacts WHERE LEN(Email) > 0 GROUP BY RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) ORDER BY EmailCount DESC, Domain Reference: https://blog.sqlauthority.com/2011/06/18/sql-server-selecting-domain-from-email-address/ All information on this site is shared with the intention to…

Read More

Windows CMD – Disk Partition and Format with DiskPart

Posted on December 22, 2016December 22, 2016

NOTE: This is not put in code as it can be VERY difficult reverse the process below. ONLY do this if you have a clue of what you are doing . This process has been tested on a USB Hard Drive, Thumb Drive, and Physical (Internal to computer) Hard Drive…

Read More

UNIX – Extract RedHat/CentOS/Fedora RPM File

Posted on April 21, 2016April 21, 2016

I love to find RPM files that someone has compiled so that I don’t have to do the manual work but when I find one at a questionable location or one I’m simply not familiar with it’s nice to know what is in it. Recently I did a search on…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes