Can someone please tell me why the results of my query are not appearing in my console application? I have a feeling it is something wrong with the console.writeline or readline

Imports System.Data
Imports System.Data.SqlClient

Module Module1

    Private Property Sql As String

    Sub Main()
        Dim ConnString As String = "Data Source=10.5.1.9;Initial Catalog=AddressStagingTable;Persist Security Info=True;User ID=sa;Password=JFK9j0b"

        Using Con As New SqlConnection(ConnString)
            Con.Open()
            Sql = "Insert into Address(streetname) values(@streetname)"
            Dim cmd1 As New SqlCommand(Sql, Con)
            cmd1.Parameters.Add("@streetname", SqlDbType.VarChar, 22)
            cmd1.Parameters("@streetname").Value = "KENTUCKY ST N"
            cmd1.ExecuteNonQuery()
            'Insert New Record
            cmd1.Parameters("@streetname").Value = "SMITH ST N"
            cmd1.ExecuteNonQuery()
            'Read Records

   
            Sql = "select * from address where itemtype = 'R2' "
            Dim cmd As New SqlCommand(Sql, Con)
            Console.WriteLine(cmd.ExecuteScalar())
            Console.ReadLine()
        End Using


    End Sub

End Module

Recommended Answers

All 3 Replies

Member Avatar for Unhnd_Exception

Use a reader

dim reader as sqldatareader = cmd.executereader
do while reader.read
   console.writeline(reader("streetname"))
loop

What if I wanted to view all columns in the following query: select * from address where itemtype = 'R2'

Member Avatar for Unhnd_Exception

I don't know what your columns are but all columns you select in the query can be accessed in the reader like above.

Console.writeline(reader("StreetName") & " " & reader("streetNumber") & " " & reader("State"))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.