VB.NET Database Tutorial - Connecting To Microsoft SQL Server (PART 1) (Visual Basic .NET)

VB.NET Database Tutorial - Connecting To Microsoft SQL Server (PART 1) (Visual Basic .NET)


SERIES PART 1:
In this beginner Visual Basic .NET tutorial, I'll show you how to establish a successful connection to a Microsoft SQL Server Database using connection strings. We'll also being using TRY / CATCH to help determine the cause of connectivity problems.

Tutorial Features:
SqlConnection - Establish a SQL Database Connection
TRY / CATCH - Capturing errors and exceptions
Functions - Test a condition and return success or failure
Classes - Design a reusable class object

TIP JAR: http://www.aardaerimus.com/tipjar.asp

PROJECT UPDATE [2/18/2016]:
Improved SQLControl Class: https://www.dropbox.com/s/qv2ywbrfsjp...
Closed Caption:

welcome to the vb toolbox in this
tutorial I'll be showing you how to
establish a connection to a Microsoft
sequel database using visual basic.net
for this tutorial is it is expected that
you already have a sequel server that is
operational and accessible and the
necessary credentials to log in to that
server
what i'll be using in my environment is
Microsoft sequel server 2008 express and
I've set up a test database here called
sequel apps as well as a basic forum
structure including members table form
boards threads and replies and as you
can see I can from my management studio
i can already query that database it is
functional and working so what I want to
do is first off just establish a
connection to the database from my
application
so let's go ahead and get started I'm
going to go back to my visual studio
start page here
from here I'm going to create a new
project and it will be a windows windows
forms application i'm going to call it a
sequel app
hit OK and that will generate a project
template for us from here and what I'm
going to do is create a sequel
connection class you can put all your
sequel code in your main form if you
want i just like to get to break it out
to connect kind of keep it clean
so in our solution Explorer if you can't
see that click view and solution
Explorer should be able to get to that
click on your project header and then
right click and add a new class and i'm
going to name my class sequel control
SQL and drill them then i'll click add
that creates a new class outline for us
now the first thing to connecting to a
sequel database is making sure that you
have imported the proper library so
going to start by saying imports system
God SQL I'm sorry . data that SQL and
then imports system.data . SQL client
all right and with those libraries
accessible we can now create a sequel
connection and our sequel command to
connect to our database and queria so
we'll start out by adding a public SQL
con that will be our connector as a new
sqlconnection and we're going to have to
supply it
some values you can do it either
directly here or in a new sub
I'm just going to do it right here and
kind of kill two birds with one stone so
i'm going to click with and open some
curly braces and supply a connection
string and this connection string is
very important for establishing our
connection
so we have to supply at least four
parameters here to get a connection to
our database with one server pointer and
then a database pointer and then we have
to supply our credentials for connecting
to that database
so first thing we'll do is supply a
server and your server is going to be
your server in instance that you're
trying to connect to in my case my
server instance is on an old minecraft
the box and so it's minecraft backslash
sequel expresses my instance so that
will be my server name
so I'll say minecraft backslash SQL
Express
now each parameter in this string will
be separated by semicolons so we'll add
a semicolon after that and now we will
point to our database that we want to
connect to in my case the database is
SQL apps
ok so I'm going to say database equals
SQL apps
all right and finally we need to supply
a user and password with proper
credentials to access our database so
i'm going to be using my default sequel
server admin account so what I'm going
to do is I'm going to say user equals si
that's the default account and then I
just need to supply password the
password parameters PWD and set this to
equal whatever your password is to
connect mr. cast for the one not very
secure but that's just me
perfect for a test so I
I'm going to finalize that with one last
semicolon not sure that last one is
absolutely required but i like to put it
in there anyway so then I'm going to
drop down and say public SQL command and
this is what you use for a Quarian your
database after you've successfully
connected to it
so to say as SQL command our command
sorry
we probably won't actually be using that
name in this part but in the next
segment we will so I'm just going to add
it now
finally to test whether our connection
is successful or not we're going to
create a function and it's a public
function as a connection and i'm just
going to return this as a boon value so
i'm going to say as in and in this
function what will do is we will simply
a test our SQL con our connection string
and see if it successfully opens and
closes if not we're going to fire back
an exception message to tell us why it
failed and to do this what we're going
to use is a try catch and this allows an
error to occur without actually crashing
your application its you can catch the
exceptions and return them gracefully
and then return back to your application
if it fails so let's go ahead and just
test our sequel connection missing SQL
con . open now if everything we've
supplied here is accurate then this
should not fail
and if it is successful then let's go
ahead and clean up after ourselves and
close that connection
now if there is an error we're going to
want to return that value or we're going
to want to at least have maybe some way
of catching that you can dump it to a
log file or something i'm just going to
say that show me a message box telling
me why it failed
so to do that will sit will capture the
exception . message so we'll say EXT
message now as a function we need to
return a value so i'm going to say if
the open and close occurs successfully
having a savory return true otherwise
return plots
alright so let's go ahead and test this
connection and see what happens see if
my authentication information here is
accurate to do this i'm going to go back
to my main form and i'm just going to
create a new instance of this sequel
control class that we created but to do
that I'm just going to say them
SQL has new a new instance of SQL
control
ok so we're going to create a new
instance of our sequel control class
that we created and then we can use that
to call
that has connection function
so what we'll do is we'll say if SQL
that has connection equals true then
returned to us a message box that says
successfully connected
let's go ahead and run that and see if
it works
might take me a moment here this old box
that I'm using is a sequel server is
incredibly old and slow so as you can
see the connection was a success it was
able to connect to the database and
close its connection successfully so
let's go ahead and force it to crash
I'm gonna go back to my sequel control
and deliberately set one of these values
to an incorrect value
I'm just going to put addy my username
and then i'm going to run that again and
it immediately fail they tried to log in
and says it failed for user said so my
catch it
exception work properly and then return
back to my main form without actually
killing my application
so as you can see it was a complete
success and we can also try altering
another parameter here let's mess up the
database connection and put a team in
from that run that
and boom it cut another exception cannot
open the database requested by the login
so it notice that the day that was a
failure as well so and return that to
the way it was
so as you can see it's really very
simple
to connect to a sequel database now if
you stick around the next tutorial i
will show you how to query the database
and return values from it from our
fields and we'll do this in a couple of
different ways will retrieve values and
put them in a form as well as just
testing with message boxes and things
like that
there are a number of other ways to
return your data as well as populating
data grids and hopefully we'll be able
to cover that in a and upcoming tutorial
as well so i hope you found this helpful
for getting started
if this was helpful love
please share it with anybody else that
you think would find it useful and if
you like it give me a thumbs up I
appreciate that thank you for coming
along and look forward to sharing more
with you on the next step i will talk to
you later
my back

Video Length: 12:19
Uploaded By: VB Toolbox
Published: 12/5/2013
View Count: 207,938

Related Software Products
VISUAL BASIC 2005 AND DATABASES
VISUAL BASIC 2005 AND DATABASES

Published By:
KIDware

Description:
VISUAL BASIC 2005 AND DATABASES is a tutorial that provides a detailed introduction to using Visual Basic for accessing and maintaining databases for desktop applications. Topics covered include: database structure, database design, Visual Basic 2005 project building, ADO .NET data objects (connection, data adapter, command, data table), data bound controls, proper interface design, structured query language (SQL), and database reports. Actual projects developed include a sales invoice system, ...


Related Videos
Programming in Visual Basic .Net How to Connect Access Database to VB.Net
Programming in Visual Basic .Net How to Connect Access Database to VB.Net

Video Length: 19:11
Uploaded By: iBasskung (12/13/2013)
View Count: 33,430,373

How to Add,Edit,Delete&Save a data in Database-visual basic 6.0-Tamil tutorial
How to Add,Edit,Delete&Save a data in Database-visual basic 6.0-Tamil tutorial

Video Length: 23:03
Uploaded By: Ajith Kumar (6/15/2012)
View Count: 331,331

VB.NET MS Access Database Tutorial 2 # Add New ,Remove ,Save Data in Database using VB.NET
VB.NET MS Access Database Tutorial 2 # Add New ,Remove ,Save Data in Database using VB.NET

Video Length: 08:35
Uploaded By: ProgrammingKnowledge (7/6/2014)
View Count: 290,229

Visual Basic Database SEARCH/Add/Delete/Save
Visual Basic Database SEARCH/Add/Delete/Save

Video Length: 02:58
Uploaded By: THeChirpiees (1/8/2012)
View Count: 286,462

MySQL VB.NET Tutorial 6 : Insert/Save data to database
MySQL VB.NET Tutorial 6 : Insert/Save data to database

Video Length: 18:38
Uploaded By: ProgrammingKnowledge (3/17/2014)
View Count: 225,576

5.9 Displaying Database Data in Visual Basic Textboxes
5.9 Displaying Database Data in Visual Basic Textboxes

Video Length: 10:41
Uploaded By: Firchild (11/16/2010)
View Count: 194,112

Visual Basic Login Form Using Access Database Part 1
Visual Basic Login Form Using Access Database Part 1

Video Length: 06:39
Uploaded By: programmingtuts101 (6/15/2012)
View Count: 68,587

Copyright © 2026, Ivertech. All rights reserved.