0

これが私が取り組んでいるものです:

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()

ご覧のとおり、クエリの結果をコマンドラインに表示しようとしていますが、現在表示されているのは「System.Data.SqlClient.SqlDataReader」だけです。SQL クエリの結果はどこに行き、なぜそれらを取得できないのですか?

4

2 に答える 2

5

これが問題です:

Console.WriteLine(reader.ToString)

ToString現在の行から特定の値を要求するのではなく、リーダーを直接呼び出しています。何かのようなもの:

Console.WriteLine(reader.GetString(0))

大丈夫なはずです。

于 2012-04-27T16:10:00.530 に答える
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

サブ終了

于 2013-03-21T10:33:01.047 に答える