0

我想知道是否有可能以某种方式在我的 vb 代码中使用查询来按字母顺序排列我连接到数据集的组合框中的列表?

这是我的代码:

Private Sub ValueSourceAvailabilityBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ValueSourceAvailabilityBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ValueSourceAvailabilityBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ValueTrackerDataSet)

    End Sub

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ValueTrackerDataSet3.SA_CountryCode' table. You can move, or remove it, as needed.
        Me.SA_CountryCodeTableAdapter.Fill(Me.ValueTrackerDataSet3.SA_CountryCode)
        'TODO: This line of code loads data into the 'ValueTrackerDataSet2.SA_Client' table. You can move, or remove it, as needed.
        Me.SA_ClientTableAdapter.Fill(Me.ValueTrackerDataSet2.SA_Client)
        'TODO: This line of code loads data into the 'ValueTrackerDataSet1.Cubes' table. You can move, or remove it, as needed.
        Me.CubesTableAdapter.Fill(Me.ValueTrackerDataSet1.Cubes)
        'TODO: This line of code loads data into the 'ValueTrackerDataSet.ValueSourceAvailability' table. You can move, or remove it, as needed.
        Me.ValueSourceAvailabilityTableAdapter.Fill(Me.ValueTrackerDataSet.ValueSourceAvailability)

    End Sub

    Private Sub ClientIDComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClientIDComboBox.SelectedIndexChanged
        SELECT *
FROM dbo.SA_Client 
ORDER by ClientName
    End Sub
End Class
4

3 回答 3

1

您不能像这样在 VB .Net 子程序中放置一条 SQL 语句。您需要使用 LINQ/Entity Framework,或者将查询格式化为字符串并将其发送到 SQL。第一种方法取决于您的实体是如何设置的,而第二种方法看起来像这样:

'The SQL Connection
Dim sqlLConn As New SqlConnection() 
'The SQL Command
Dim sqlCmd As New SqlCommand() 

'Set the Connection String
sqlConn.ConnectionString = "connection string goes here" 
'Open the connection     
sqlConn.Open 
'Set the Connection to use with the SQL Command
sqlCmd.Connection = SQLConn 
' Set your query
sqlCmd.CommandText = "SELECT * FROM dbo.SA_Client ORDER by ClientName"  


' DO STUFF WITH THE sqlCmd query here...


'Close the connection
sqlConn.Close() 
于 2013-07-17T15:17:08.727 回答
1

第一个选项:您可以通过您的 sql 查询以编程方式执行此操作

SELECT * FROM tableName ORDER BY columnName ASC/DESC

第二个选项,将 Sorted 选项设置为 TRUE在此处输入图像描述

于 2013-07-17T15:22:56.853 回答
0

如果您从数据库中检索到的数据已经按 ClientName 顺序排序,它会解决您的问题吗?在这种情况下,您需要更改与SA_ClientTableAdapter数据适配器上的 Fill 方法关联的 SQL。

如果您需要将数据与 DataSet 和 DataTable 分开排序,您有几个选择。

  1. 您可以使用 LINQ,并绑定到数据的排序版本。与其直接绑定到 DataSet 或 DataTable,不如绑定到表达式:

    [whatever you're binding] = Me.ValueTrackerDataSet2.SA_Client.OrderBy(c=>c.ClientName)
    
  2. 您可以在 DataTable 上使用 DataView,这是我们用来执行此操作的旧方法之一:

    Dim dv as DataView = New DataView(Me.ValueTrackerDataSet2.SA_Client, "", "ClientName", DataViewRowState.CurrentRows)
    [whatever you're binding] = dv
    
于 2013-07-17T15:35:38.247 回答