ADO.NET newbie question

  • 14 years ago

    i have'nt tried ado.net yet is it very much different from ado in vb6 can someone give me a sample code like inserting a record so i can have an idea how it looks like.

  • 14 years ago

    Hi,
     There are lot of sites you can refer to start with. It will be better you have a look in MSDN for ADO and ADO.NET difference. This will help you to make the change over easier. Very nice website to start ADO.NET


    http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/ADOPlusOverview.aspx


      There are lot of ways you can do an Insert operation in ADO.NET.


       I am giving a small piece of code using two different ways to insert a record. The example uses SQL Server's Northwind DB as connection string. You can change to your Database of choice.  You can also use ADODB for .NET


    Put the following code at the start of your code window. similar place where you put Option explicit declaration in VB 6


    Imports System
    Imports System.Data
    Imports System.Data.OleDb     'This is for OleDb
    Imports Microsoft.VisualBasic
    Imports System.Data.SqlClient  'This is for SqlClient exclusively for SQL Server DB



    Put the following code in any event. Here I am using it in a Button Click Event.


         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    'This is for SQLClient


           Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _
                                                                  "Integrated Security=SSPI;Initial Catalog=northwind")


           Dim catCMD As OleDbCommand = nwindConn.CreateCommand()
           catCMD.CommandText = "Place your Insert statement"
           nwindConn.Open()
           catCMD.ExecuteNonQuery()
           nwindConn.Close()
       End Sub


       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


    'This is for OLEdb
           Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;" & _
                                                          "Integrated Security=SSPI;Initial Catalog=northwind")
           Dim catCMD As SqlCommand = nwindConn.CreateCommand()
           catCMD.CommandText = "Place your Insert statement"
           nwindConn.Open()
           catCMD.ExecuteNonQuery()
           
           nwindConn.Close()
       End Sub



    Hope this was useful.  If you need further help have a look at MSDN. It has some Samples.

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.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay