Dynamic declaration of TableAdapters for SQL database in VB (or Visual-Studio in general)

16 Views Asked by At

I have been using Stackoverflow to find some really good answers to some of the problems I have encountered, but now it is my turn to ask as I don't seem to be able to find an answer, although it seems it is not really possible, which would be a bit puzzling to me.

I have an electronics parts database, of which I am the sole creator/user. It consists of a number of tables with different component types, and new tables are being added as I find the need during designs for new and different part types from what I already in my database. The problem I have is that I am essentially lazy and I truly hate doing repetitive work that I should be able to automate.

Thus right now I have something along the lines of

Dim ResistorsTableAdapter As New PartsInventoryDataSetTableAdapters.ResistorsTableAdapter()

at the load part, which I manually have to do for each table in the database (tedious, especially as the database slowly expands) followed by

COMPONENTS_DATAGRIDVIEW.DataSource = ResistorsTableAdapter.GetData

at table (parts type) selection by using a Combobox or a search function. So I was wondering if there when connecting to the SQL table on program load is a way to dynamically allocate/declare the TableAdapters - I'm already reading out the database table titles for filling the selection combobox using the following piece of code:

        Dim i As Integer = 1
        For Each row As DataRow In ds.Tables(0).Rows
            ComboBox1.Items.Add(row.Item("TABLE_NAME"))
            ComponentTables(i).RowNumber = i                                                ' Variable holding the table number 
            ComponentTables(i).Name = row.Item("TABLE_NAME")                                ' Variable holding the table name
            ComponentTables(i).ChangedName = GenerateComponentName(row.Item("TABLE_NAME"))  ' Variable holding the name changed to conform to Visual Studio, i.e. currently both " " (space) and "/" characters changed to "_"  
            DebugBox.Text &= GenerateComponentName(row.Item("TABLE_NAME")) & " - " & ComponentTables(i).RowNumber & " " & ComponentTables(i).Name & " " & ComponentTables(i).ChangedName & " " & vbCrLf ' just for my own debugging
            i += 1
        Next row

I would be baffled if there isn't some way of doing this, as I can't be the only one out there having to deal with databases that have parts added or removed. Admittedly, I am a novice to database handling in Visual Studio, so I may just have missed something glaringly obvious.

Thanks!

0

There are 0 best solutions below