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
View Count: 132,100

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 ...


Related Videos
Visual basic database add, remove & save buttons
Visual basic database add, remove & save buttons

Create your profile and join other programmers http://www.suncoast-software.com Learn how you can use your own buttons to add, remove and save data into your database without having to use the default menu bar. Trusted hosting provider for ASP.NET for less then $0.25 a day and a free domain of your choosing. http://bit.ly/freehost20 hr / bClosed Caption:/b welcome to another visual basic video tutorial in this video I'm going to show you ...
Video Length: 07:56
Uploaded By: Networking26
View Count: 562,648

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

Free Source Code: https://www.facebook.com/CodeAMinute . Please Check Out New Video - https://www.youtube.com/watch?v=zYzgl... - Connect to an Access 2013 Database (Coding). - How to Generate Custom Auto Numbers (ID). . Programming in Visual Basic .Net with Microsoft Visual Studio 2010 Demo - How to Connect Access Database to VB.Net (access database connection) Download Link : (Rewrite Version) please visit my blog. http://goo.gl/PXe3Xgbr ...
Video Length: 19:11
Uploaded By: iBasskung
View Count: 391,849

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

Hai friends..... Please send your comments about this video after you watch.. If you want any details contact me...ajithkumar6363@gmail.com...Thank you...
Video Length: 23:03
Uploaded By: Ajith Kumar
View Count: 241,021

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

How to create a searchable database in visual basic. Access File Link: http://www.filedropper.com/addressbook
Video Length: 02:58
Uploaded By: THeChirpiees
View Count: 226,767

visual basic login using a database (SQL way)
visual basic login using a database (SQL way)

Command: command.CommandText = "SELECT * FROM [User] WHERE (username='" + TextBox1.Text + "')AND (password='" + TextBox2.Text + "');" Title says it all, want the source goto http://www.deadcore.co.uk/download/tu.... Want more goto http://www.deadcore.co.uk or goto http://www.deadcore.co.uk/forum/index...
Video Length: 09:50
Uploaded By: deafjackomonster
View Count: 124,921

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

How to integrate Access and VB using text boxes to display data and buttons to navigate the records. Here's the code: Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) HandlesMyBase.LoadOleDbDataAdapter1.Fill(DsPatientsAndWard1) txtWardName.DataBindings.Add("Text", DsPatientsAndWards1, "Patients.Ward Name") txtWardClass.DataBindings.Add("Text", DsPatientsAndWards1, "Patients.Ward Class")br ...
Video Length: 10:41
Uploaded By: Firchild
View Count: 119,057

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

How to save data in directly to the database in vb.net Saving Data to Database How to insert data Connect MS Access Database (.mdb) with VB.NET How to Add, save & retrieve data in SQL Server using VB.NET Help cant save data to sql database in VB.NET How to: Insert New Records into a Database Database Access with Visual VB.NET.NET sql server - Insert Data into database inVB.NET How to insert Data to Ms SQL server 2008 using C#.net .net - VB.NET ...
Video Length: 08:35
Uploaded By: ProgrammingKnowledge
View Count: 95,898

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

How to: Insert New Records into a Database Database Access with Visual VB.NET.NET sql server - Insert Data into database in VB.NET How to insert Data to Ms SQL server 2008 using VB.NET.net .net - VB.NET insert data into SQL table Using Visual VB.NET and SQL cannot insert data into table..‎ VB.NET import data from text file into sql databse line by line‎ store xml in SQL Server 2008 from VB.NET‎ Using SQL in VB.NET - Cannot insert data into SQL tablebr ...
Video Length: 18:38
Uploaded By: ProgrammingKnowledge
View Count: 92,612

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

IF YOU DONT HAVE ANYTHING CONSTRUCTIVE TO SAY THEN DON'T SAY IT!! For Part 2 got to http://www.youtube.com/watch?v=sDdTOy... Text file to follow: http://www.mediafire.com/view/?3acbg3... Final Project: DO NOT JUST COPY AND PASTE http://www.mediafire.com/download.php... hr / bClosed Caption:/b I guess today i will be showing you how to create a visual basic login form using the AVO DB connection method i'mbr ...
Video Length: 06:39
Uploaded By: programmingtuts101
View Count: 56,806

Copyright © 2025, Ivertech. All rights reserved.