Visual Studio 2013: Connecting to database SQL Server 2014 using Visual Studio 2013 form
In this tutorial we will create an application to testing the SQL Server 2014 database connection. We will use connection string in the connection script. This part of lesson is very important for you which want to learn database programming. Before you can manipulate the database (SQL) using Windows form, you have to know how to connect them to the database.
There is several steps to connecting to the database, that is:
First, you have to open Visual Studio 2013, click New Project. Choose Windows Form Application. Then will appear New Project with default 1 Form on the workspace.
>> Add Command Button on the Form
Add a command button control take from Tool Box to the form object.
>> Add Module to the Project
To add Module to the Project you can click the menu Project-Add Module-Choose Module-Give the Module Name-click Add.
>> Write Command Script to each Object
We will write some script lines to the Command Button and Module. The script will execute if the command button being clicked. Here the script:
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
Try
opendb()
MsgBox("Connection succeeded...!!!")
Catch ex As Exception
MsgBox("Connection failed...!!!")
End Try
End Sub
>>Module Script
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Public oConn As OleDb.OleDbConnection
Public Sub opendb()
oConn = New OleDb.OleDbConnection("Provider=SQLOledB.1;" & vbCrLf & _
"Initial catalog=DB2015;" & vbCrLf & _
"Password=1234567; " & vbCrLf & _
"User ID=sa;data source=.")
oConn.Open()
End Sub
End Modul
>> Test the application
Last, you can test the application. If your application succeeded, than will appear a message box "Connection succeeded...!!!". For more steps watch the tutorial video above.
Thank you.
There is several steps to connecting to the database, that is:
- Create Visual Studio 2013 New Project
- Choose Windows Form Application
- Add Command Button on the Form
- Add Module to the Project
- Write command script to each Object
- Test the application
First, you have to open Visual Studio 2013, click New Project. Choose Windows Form Application. Then will appear New Project with default 1 Form on the workspace.
Add a command button control take from Tool Box to the form object.
>> Add Module to the Project
To add Module to the Project you can click the menu Project-Add Module-Choose Module-Give the Module Name-click Add.
>> Write Command Script to each Object
We will write some script lines to the Command Button and Module. The script will execute if the command button being clicked. Here the script:
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
Try
opendb()
MsgBox("Connection succeeded...!!!")
Catch ex As Exception
MsgBox("Connection failed...!!!")
End Try
End Sub
>>Module Script
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Public oConn As OleDb.OleDbConnection
Public Sub opendb()
oConn = New OleDb.OleDbConnection("Provider=SQLOledB.1;" & vbCrLf & _
"Initial catalog=DB2015;" & vbCrLf & _
"Password=1234567; " & vbCrLf & _
"User ID=sa;data source=.")
oConn.Open()
End Sub
End Modul
>> Test the application
Last, you can test the application. If your application succeeded, than will appear a message box "Connection succeeded...!!!". For more steps watch the tutorial video above.
Thank you.
Comments
Post a Comment