Database connection (manually)

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


  • 12 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:


    Code:

    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



    Code:

    ' (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
    DataGrid1.DataBind()



    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.



    GMike    


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

Contribute

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.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou