Database connection (manually)

  • 13 years ago

    In VB.NET while connecting to database we use SQLdataadapter or oledbdadapter, which help us to connect the database through wizard.

    But can we implement this by writing code, so that database can be connected. Is this possible. Yes , then how to write code.

  • 13 years ago

    Drag and Drop functionalities and Wizards are just additional rapid application development features given by Visual Studio.Net, all ADO.NET objects are available in code also and interanally VS.NET generates coding for various procedures created by Wizards.

    Here i am showing manual way to implement database, i am using OLEDB classes which can be replaced by SQL calsses also:


    Imports System.Data
    ' For Microsoft SQL Server database connections.
    Imports System.Data.SqlClient

    ' For Oracle database connections.
    Imports System.Data.OracleClient

    ' For other database connections.
    Imports System.Data.OleDb


    ' (1) Create the data connection.
    Dim ContactMgmt As New SqlConnection ("server=(local);database=Contacts;Trusted_Connection=yes")

    ' (2) Create a data adapter.
    m_adptContactMgmt = New SqlDataAdapter("select * from Contacts", ContactMgmt)

    ' (3) Create a data set.
    Dim dsContacts As New DataSet

    ' (4) Fill the data set.
    m_adptContactMgmt.Fill(dsContacts, "Contacts")

    ' (5) Display the table in a data grid using data binding.
    DataGrid1.DataSource = dsContacts.Tables("Contacts").DefaultView

    Like this you can implement any database related operation through coding although it takes time but it will highly increase the flexibility and undersating of your code.


    "I would love to change the world, but they won't give me the source code."

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie