SQL选择Vs二进制搜索

时间:2015-10-01 08:03:31

标签: sql vb.net linq search

仅为了记录我将要添加的未来问题 将数据提取到datagridview,我知道代码远非一目了然,只是为了理解这个主题的属性。

    CreateDataTable(Me.t, "SELECT * FROM tblContacts", "tblContacts")
    CreateDataTable(Me.t2, "SELECT * FROM ContactsData", "ContactsData")

    Dim joined = From row1 In t.AsEnumerable()
                   Join row2 In t2.AsEnumerable()
                   On row1.Field(Of Int32)("ID") Equals row2.Field(Of Int32)("ID")
                   Select BothRows = New With {row1, row2}






    With DataGridView1
        .Columns.Clear()
        .Rows.Clear()
        ' add columns of tblContacts table
        For i = 0 To joined.AsEnumerable.First.row1.Table.Columns.Count - 1
            .Columns.Add(joined.AsEnumerable.First.row1.Table.Columns(i).ColumnName.ToString, joined.AsEnumerable.First.row1.Table.Columns(i).ColumnName.ToString)
        Next
        ' add columns of ContactsData table
        For i = 0 To joined.AsEnumerable.First.row2.Table.Columns.Count - 1
            .Columns.Add(joined.AsEnumerable.First.row2.Table.Columns(i).ColumnName.ToString, joined.AsEnumerable.First.row2.Table.Columns(i).ColumnName.ToString)
        Next
        ' add rows to datagridview
        .Rows.Add((joined.Count - 1))

    End With

    Dim rownum As Integer = 0
    For Each x In joined

        Dim row1 As DataRow = x.row1
        Dim row2 As DataRow = x.row2

        ' populate data to dgv
        For i = 0 To row1.Table.Columns.Count - 1
            DataGridView1.Rows(rownum).Cells(i).Value = row1.Item(i).ToString
        Next

        For i = row1.Table.Columns.Count To DataGridView1.Columns.Count - 1
            DataGridView1.Rows(rownum).Cells(i).Value = row2.Item(i - row1.Table.Columns.Count).ToString
        Next

        rownum += 1
    Next

经过测试@Tim的回答如下: 我已经测试了linq到数据集,但我得到一个错误,我无法弄清楚为什么.. 错误描述是'值不能为空。参数名称:source' 以防万一我已经测试了一个常规的SQL内连接查询,并且工作正常。

我在访问中创建了2个表,这是表

 tblCOntacts
 --------------
 ID FirstName   LastName
 ---------------------------------
 22 John        Smith
 23 Jane        Apple
 24 Jonathan    Tucker


 ContactsData
 --------------
 ID Proffesion  Hoby
 ---------------------------------
 22 Carpenter   Long Runs
 23 Pilot       Gym
 24 Psychologyst    Programming

这是我的代码,我评论了导致错误的行:

 Private Sub Button4_Click(sender As System.Object, e As System.EventArgs)           Handles Button4.Click
     CreateDataTable(Me.t, "SELECT * FROM tblContacts", "tblContacts")
    CreateDataTable(Me.t2, "SELECT * FROM ContactsData", "ContactsData")

   Dim joined = From row1 In t.AsEnumerable()
                  Join row2 In t2.AsEnumerable()
                  On row1.Field(Of Int32)("ID") Equals row2.Field(Of Int32)("ID") ' here i get the error: Value cannot be null. Parameter name: source
                  Select BothRows = New With {row1, row2}

    For Each x In joined
        Dim row1 As DataRow = x.row1
        Dim row2 As DataRow = x.row2
    Next
 end sub


   Public con As OleDbConnection
   Public dset As DataSet
   Public ad As OleDbDataAdapter
   Public cmd As OleDbCommand
   Public t As DataTable
Public t2 As DataTable

  Private Sub CreateDataTable(ByVal tb As DataTable, ByVal query As String,   ByVal table As String)
         Dim q As String = query
    con = New OleDbConnection
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Jonathan\Desktop\בסיס נתונים access\AddressBook.mdb"

    con.Open()
    dset = New DataSet
    cmd = New OleDbCommand(query, con)
    ad = New OleDbDataAdapter(cmd)
    ad.Fill(dset, table)
    con.Close()

    ' query dataset with linq
    tb = dset.Tables(table)
 end sub

我有来自不同Access数据库的2个dataTable(所以我不能使用SQL连接steatments),我需要通过相同的数字主键匹配来自2个表的记录中的2列。 我正在考虑两个选择:

  1. 多个SQL SELECT查询。
  2. 使用visual basic对2个数据表对象进行二进制搜索。
  3. 哪种方式更有效率?更专业?这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

效率方面的最佳方法 - 更重要的 - 可读性和可维护性是使用Linq-To-DataSet

function optionClicked () {

  var price = parseInt($('#phone').val()) || 700;
  var terms = [12, 11];
  var durations = [6, 12, 18, 24];
  var plans = [
    parseInt($('#bb1').val()) || 600,
    parseInt($('#bb2').val()) || 500,
    parseInt($('#bb3').val()) || 899,
    parseInt($('#bb4').val()) || 900
  ];

  var term = $('.selected-term').parent().index();
  var plan = $('.selected-cplan').parent().index();
  var duration = $('.selected-month').parent().index();

  var total = (price / terms[term]) + (plans[plan] / durations[duration])
  var monthly = total / 12;
  var weekly = total / 52;

  $('#total').val(total);
  $('#total12').val(monthly);
  $('#total52').val(weekly);

}

您需要添加Dim joined = From row1 In table1.AsEnumerable() Join row2 In table2.AsEnumerable() On row1.Field(Of Int32)("ID") Equals row2.Field(Of Int32)("ID") Select BothRows = New With {row1, row2} For Each x In joined Dim row1 As DataRow = x.row1 Dim row2 As DataRow = x.row2 Next Enumerable.Join是有效的,因为它使用了一个集合。