0

我有一个SQL Server 2008带有数据库的快递和一个table使用快递的VB 2010快递。

我正在尝试从该表中读取sqldatareader,但我只有一行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

您没有将数据作为一个组读取(您只获取一个结果)。

您需要调整代码以使用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

类似的东西应该可以工作(我没有测试过代码,但概念是一样的)。

PS - 我强烈建议您调整脚本以添加Where子句和/或所需的列(选择 * 不是“好习惯”)

希望这可以帮助。

于 2013-09-03T11:26:50.640 回答