0

Here is what I'm working with:

Dim connstr = "data source=mydatasource;initial catalog=gcs_dw;persist security   info=True;user id=myuser;password=mypassword;Asynchronous Processing=True"
Dim sqlquery = "SELECT * FROM Customer WHERE CITY = 'Anytown'"
Dim connection As SqlConnection = New SqlConnection(connstr)
connection.Open()

Dim command As SqlCommand = connection.CreateCommand()
command.CommandText = sqlquery
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
Console.WriteLine(reader.ToString)
connection.Close()
Console.Read()

As you can see, I'm attempting to display the results of the query to the commandline and currently all it is displaying is "System.Data.SqlClient.SqlDataReader". Where are the results of my sql query going and why can't I retrieve them?

2 Answers 2

5

This is the problem:

Console.WriteLine(reader.ToString)

You're calling ToString directly on the reader, rather than asking it for a specific value from the current row. Something like:

Console.WriteLine(reader.GetString(0))

should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew that .ToString was doing exactly what it was suppose it, I simply didn't know what else to use. Both .GetString and .GetValue worked perfectly. Thanks!
0
Dim str As String

Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
                                                "uid=sa;pwd=;database=master")

str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
      "(NAME = MyDatabase_Data, " & _
      " FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
      " SIZE = 2MB, " & _
      " MAXSIZE = 10MB, " & _
      " FILEGROWTH = 10%) " & _
      " LOG ON " & _
      "(NAME = MyDatabase_Log, " & _
      " FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
      " SIZE = 1MB, " & _
      " MAXSIZE = 5MB, " & _
      " FILEGROWTH = 10%) "

Dim myCommand As SqlCommand = New SqlCommand(str, myConn)

Try
    myConn.Open()
    myCommand.ExecuteNonQuery()
    MessageBox.Show("Database is created successfully", _
                    "MyProgram", MessageBoxButtons.OK, _
                     MessageBoxIcon.Information)
   Catch ex As Exception
       MessageBox.Show(ex.ToString())
   Finally
       If (myConn.State = ConnectionState.Open) Then
           myConn.Close()
       End If
   End Try

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.