Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. query: Oledb database and a BindingList

query: Oledb database and a BindingList

Scheduled Pinned Locked Moved C#
databasehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharp source
    wrote on last edited by
    #1

    goal: Display all records from a specified table in a Visual Foxpro database in a DGV. This would not be a problem, except, i only want those rows which have a record id contained by a BindingList TableId I've tried this: OledbConnection = new OleDbConnection(connectionString); OledbCommand = new OleDbCommand(query,connection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connection); foreach(string id in TableId) { query = "SELECT * FROM " + tableName + " WHERE (" + tableRecordIDColumn + " = '" + id + "') ORDER BY " + tableRecordIDColumn; command.CommandText = query; dataAdapter.SelectCommand = command; dataAdapter.Fill(dataTableFromDb); } bindingSource1.DataSource = dataTableFromDb; dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = bindingSource1; Allthough this works, it's extremely slow. Especially since my BindingList TableId could contain 20 000 record id's. Would there be any way to create a dataset with two tables on wich i could excecute a select query wich an inner join on the record id's?? And then use the results as the DataGridView.DataSource? grts, thx

    A 1 Reply Last reply
    0
    • S sharp source

      goal: Display all records from a specified table in a Visual Foxpro database in a DGV. This would not be a problem, except, i only want those rows which have a record id contained by a BindingList TableId I've tried this: OledbConnection = new OleDbConnection(connectionString); OledbCommand = new OleDbCommand(query,connection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connection); foreach(string id in TableId) { query = "SELECT * FROM " + tableName + " WHERE (" + tableRecordIDColumn + " = '" + id + "') ORDER BY " + tableRecordIDColumn; command.CommandText = query; dataAdapter.SelectCommand = command; dataAdapter.Fill(dataTableFromDb); } bindingSource1.DataSource = dataTableFromDb; dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = bindingSource1; Allthough this works, it's extremely slow. Especially since my BindingList TableId could contain 20 000 record id's. Would there be any way to create a dataset with two tables on wich i could excecute a select query wich an inner join on the record id's?? And then use the results as the DataGridView.DataSource? grts, thx

      A Offline
      A Offline
      AikinX
      wrote on last edited by
      #2

      sharp source, the problem is: you do to many connections to DB. I can propose two different solutions: 1) We can include all of this in one SQL: 'OR' version: string queryCondition = ""; foreach (string id in TableId) { queryCondition += tableRecordIDColumn + " = '" + id + "' OR "; } // We need to cat ' OR ' from the end of the string (4 letters) if (queryCondition != "") queryCondition = queryCondition.Remove(queryCondition.Length - 4); string query = "SELECT * FROM " + tableName + " WHERE (" + queryCondition + "') ORDER BY " + tableRecordIDColumn; 'In' version: string queryCondition = ""; foreach (string id in TableId) { queryCondition += "'" + id + "', "; } // We need to cat ', ' from the end of the string (2 letters) if (queryCondition != "") queryCondition = queryCondition.Remove(queryCondition.Length - 2); queryCondition = tableRecordIDColumn + " in (" + queryCondition + ")"; string query = "SELECT * FROM " + tableName + " WHERE (" + queryCondition + "') ORDER BY " + tableRecordIDColumn; Remark: You can get an exception like "the query is too long" (because of 2000 :omg: entries). You can split them to 100th packs, for example 2) Restricting the logic of data getting (I think it is better) I think you can find an way to get more simple selecting condition >>Would there be any way to create a dataset with two tables on wich i could excecute a select query wich an inner join on the record id's?? You can. You can load all data to data. Then you just need to filter the data by using BindingSource class. But this solution is valid when you have to filter data many times per minute. -- modified at 9:13 Thursday 7th June, 2007

      S 1 Reply Last reply
      0
      • A AikinX

        sharp source, the problem is: you do to many connections to DB. I can propose two different solutions: 1) We can include all of this in one SQL: 'OR' version: string queryCondition = ""; foreach (string id in TableId) { queryCondition += tableRecordIDColumn + " = '" + id + "' OR "; } // We need to cat ' OR ' from the end of the string (4 letters) if (queryCondition != "") queryCondition = queryCondition.Remove(queryCondition.Length - 4); string query = "SELECT * FROM " + tableName + " WHERE (" + queryCondition + "') ORDER BY " + tableRecordIDColumn; 'In' version: string queryCondition = ""; foreach (string id in TableId) { queryCondition += "'" + id + "', "; } // We need to cat ', ' from the end of the string (2 letters) if (queryCondition != "") queryCondition = queryCondition.Remove(queryCondition.Length - 2); queryCondition = tableRecordIDColumn + " in (" + queryCondition + ")"; string query = "SELECT * FROM " + tableName + " WHERE (" + queryCondition + "') ORDER BY " + tableRecordIDColumn; Remark: You can get an exception like "the query is too long" (because of 2000 :omg: entries). You can split them to 100th packs, for example 2) Restricting the logic of data getting (I think it is better) I think you can find an way to get more simple selecting condition >>Would there be any way to create a dataset with two tables on wich i could excecute a select query wich an inner join on the record id's?? You can. You can load all data to data. Then you just need to filter the data by using BindingSource class. But this solution is valid when you have to filter data many times per minute. -- modified at 9:13 Thursday 7th June, 2007

        S Offline
        S Offline
        sharp source
        wrote on last edited by
        #3

        Your 2 first suggestions i allready tried. And as you say, i need to limit the data getting. Btw, it's not 2000 but 20 000 :wtf: rows.... So restricting the logic of data getting. How would i do that? So i create a dataset Add two DataTables OledbDataAdapter.Fill(DataTable1); How do i get the BindingList TableId into a table? And after I have a dataset with the two tables... I gues i can figure that out. Thx

        A 1 Reply Last reply
        0
        • S sharp source

          Your 2 first suggestions i allready tried. And as you say, i need to limit the data getting. Btw, it's not 2000 but 20 000 :wtf: rows.... So restricting the logic of data getting. How would i do that? So i create a dataset Add two DataTables OledbDataAdapter.Fill(DataTable1); How do i get the BindingList TableId into a table? And after I have a dataset with the two tables... I gues i can figure that out. Thx

          A Offline
          A Offline
          AikinX
          wrote on last edited by
          #4

          >> And as you say, i need to limit the data getting. Do you want to get a code with splitting by 1000 (for example) Ids? >> So restricting the logic of data getting. I meant "restructuring", sorry. So change the logic of the app. To group some ID into groups... I need to see you task to give you more ideas. >> How do i get the BindingList TableId into a table? >>And after I have a dataset with the two tables... I gues i can figure that out. No, you will have one table with full data in it. But you will display only necessary information. To do that you have to bind you table to DataGridView (for example) through BindingSource: BindingSource bs = new BindingSource(someDataSet, "SomeMember"); someDataGridView.DataSource = bs; // then in an other place you can filter: Filter condition, like: bs.Filter = "SomeColumn = 'id1' OR SomeColumn = 'id2' OR ..... ";

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups