0

SQL Server 2008データベースとエクスプレスtableを使用したVB 2010エクスプレスがあります。

でそのテーブルから読み取ろうとしていますが、列ヘッダーのsqldatareaderある行は 1 行だけで、データのある行はありません。datagridview

私は何を間違っていますか?(私は初心者です)。

接続文字列は次のとおりです。 Data Source=xxxxxxxxxx\SQLEXPRESS;Initial Catalog=Masteca_Inventory;Integrated Security=True

Dim searchStr As String = ""
Dim connetionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sqlStr As String

Public bindingSource1 As New BindingSource()

connetionString = My.Settings.SQLconnSTR
sqlStr = "SELECT * FROM Piese WHERE " & searchStr  'searchStr is OK I fill it elsewhere
sqlCnn = New SqlConnection(connetionString)

Try
    sqlCnn.Open()
    sqlCmd = New SqlCommand(sqlStr, sqlCnn)

    Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()

    Using sqlReader

        Dim dTable As New DataTable
        dTable.Load(sqlReader)
        bindingSource1.DataSource = dTable

    End Using

    SearchReport.DataGridView1.DataSource = bindingSource1
    'SearchReport is another form

    sqlReader.Close()
    sqlCmd.Dispose()
    sqlCnn.Close()

    SearchReport.Show()

Catch ex As Exception
    MsgBox(ex.ToString)
End Try
4

1 に答える 1

1

データをグループとして読み取っていません (結果を 1 つだけ取得しています)。

WhilesqlReader.Read;を使用するようにコードを調整する必要があります。

例;

Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()

While sqlReader.Read()
    Try                      
       'Do the work needed 
       rowResult += sqlReader(0) 'This will contain the result script
    Catch ex As Exception
       'Catch exception
    End Try

End While

そのようなものは機能するはずです(コードはテストしていませんが、概念は同じです)。

WherePS - スクリプトを調整して、必要な句や列を追加することを強くお勧めします(Select * は「良い習慣」ではありません)。

お役に立てれば。

于 2013-09-03T11:26:50.640 に答える