ARTICLE

Creating Sql and ODBC Command Objects in ADO.NET

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET April 12, 2010
In this article I will explain Creating Sql and ODBC Command Objects in ADO.NET.
 
Reader Level:

Similar to the OldDbCommand object, you create Sql and ODBC Command objects by using SqlCommand and OdbcCommand classes. You can pass the same arguments as discussed previously. The only difference is the connection string. For Example, listing 5-31 uses SqlCommand and SqlConnection to connect to SQL server database. As you can see from listing 5-31, the only changes are the class prefixes and the connection string. Similarly, you can use the OdbcCommand object.

Listing 5-31. Using SqlCommand to access a SQL Server database


Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data.SqlClient

Namespace ConsoleApplication1
    Class Program
        Private Shared Sub Main(ByVal args As String())

            ' Connection and SQL strings
            Dim SQL As String = "SELECT * FROM Orders"

            ' create a connection object
            Dim ConnectionString As String = "Integrated security =SSPI;" & "Initial Catalog = Northwind;" & "Data Source = MAIN-SERVER; "
            Dim conn As New SqlConnection(ConnectionString)

            ' create command object
            Dim cmd As New SqlCommand(SQL, conn)

            ' open connection
            conn.Open()

            ' call command's ExcuteReader
            Dim reader As SqlDataReader = cmd.ExecuteReader()

            Try
                While reader.Read()
                    Console.Write("OrderID:" & reader.GetInt32(0).ToString())
                    Console.Write(" ,")
                    Console.WriteLine("Customer : " & reader.GetString(1).ToString())
                End While
            Finally

                ' close reader and connection
                reader.Close()
                conn.Close()
            End Try
        End Sub
    End Class
End Namespace

The output of Listing 5-31 looks Figure 5-31.

Figure-5.31.jpg

Figure 5-31. Output of listing 5-31


Conclusion

Hope this article would have helped you in understanding
Creating Sql and ODBC Command Objects in ADO.NET. See my other articles on the website on ADO.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor