使用VB .net从Sqlserver db中检索记录

时间:2021-12-03 01:40:26

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?

正如您所看到的,我正在尝试将查询结果显示到命令行,目前显示的只是“System.Data.SqlClient.SqlDataReader”。我的SQL查询的结果在哪里,为什么我无法检索它们?

2 个解决方案

#1


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:

您直接在阅读器上调用ToString,而不是从当前行询问它是否具有特定值。就像是:

Console.WriteLine(reader.GetString(0))

should be fine.

应该没事。

#2


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

结束子

#1


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:

您直接在阅读器上调用ToString,而不是从当前行询问它是否具有特定值。就像是:

Console.WriteLine(reader.GetString(0))

should be fine.

应该没事。

#2


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

结束子