d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Vb.net + Mysql Question
Add Reply New Topic New Poll
Member
Posts: 6,990
Joined: Apr 16 2019
Gold: 50.00
Jan 8 2020 11:14am
Table Structure

I use a "one-to-many" kind of table structure. Instead of storing all information in one table, I like to split it up.

For instance, I work in the battery profession and we record a lot of unique characteristics of a battery each time we service it.

Example;

Connector Type
Case Color
Cable Length
Cable Size
Model Number
ect. ect.

All in all, I have 22 pieces of information I record each time a new battery is inserted.

So this is how the process works;

Table #1 Systems

Structure

SystemID
SerialNumber

Batteries all have unique Serial Number so that is my Index. When I enter a new "System" IE Battery it goes into a record like this

SerialNumber = "00001"
SystemID = "Battery-00001"

(Identifier because we also work with the chargers, so I need a way to identify)

Table #2 SystemsAllocatedParts

SystemID
PartID
Data

So each time a new battery + System is added, we record those 22 items, those 22 items are now stored in SystemsAllocatedParts that way I can run a query against the SystemsAllocatedParts and get all information in one place.

IE;

SystemID = "Battery-00001"
PartID = "BatteryData-ConnectorType"
Data = "SB 350 Gray"

So for each of these items I need to do an INSERT statement to put it into AllocatedParts. I usually handle INSERTS like this

Code

Statement = "INSERT INTO SystemsAllocatedParts (SystemID, PartID, Sensor) VALUES (@Sys, @Part, @Data)"
Using con As New MySqlConnection(DBConnection)
Using cmd As New MySqlCommand(Statement)
Using sda As New MySqlDataAdapter()
con.Open()
cmd.Parameters.AddWithValue("@Sys", "Battery-" & SerialNumberToUse)
cmd.Parameters.AddWithValue("@Part", "BatteryData-UnitNumber")
cmd.Parameters.AddWithValue("@Data", unitnumberBox.Text)
cmd.Connection = con
sda.InsertCommand = cmd
cmd.ExecuteNonQuery()
con.Close()

End Using
End Using
End Using


But I do NOT want to make 22 insert statements this way.

Does anyone know of a way I can handle all of these, where I change the PartID + the Data for each of the items?

Willing to pay upwards of 500FG for a good answer. PM me if anything doesn't make sense.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Jan 8 2020 12:58pm
Can you make a small drawing of your table layout like this example? https://online.visual-paradigm.com/app/diagrams/#diagram:proj=0&type=ERDiagram&gallery=/repository/26696f29-1f6a-44a2-adb4-b03cff92e492.xml&name=Inventory%20System

If all you care about is not hitting the database with multiple queries, you can build the insert statement as such:

"INSERT INTO SystemsAllocatedParts (SystemID, PartID, Sensor) VALUES ('Battery-00001', 'BatteryData-ConnectorType', 'SB 350 Gray'), ('Battery-00002', 'BatteryData-ConnectorType', 'SB 300 Blue'), ..., ('Battery-0000n', 'BatteryData-ConnectorType', 'SB 9999 White');"

https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlParameterCollection_AddRange.htm

you would have to use that instead of named variables, not sure exactly how since I haven't used that interface before (and I don't program vb.net..)
Member
Posts: 6,990
Joined: Apr 16 2019
Gold: 50.00
Jan 8 2020 03:21pm
Quote (Klexmoo @ Jan 8 2020 01:58pm)
Can you make a small drawing of your table layout like this example? https://online.visual-paradigm.com/app/diagrams/#diagram:proj=0&type=ERDiagram&gallery=/repository/26696f29-1f6a-44a2-adb4-b03cff92e492.xml&name=Inventory%20System

If all you care about is not hitting the database with multiple queries, you can build the insert statement as such:

"INSERT INTO SystemsAllocatedParts (SystemID, PartID, Sensor) VALUES ('Battery-00001', 'BatteryData-ConnectorType', 'SB 350 Gray'), ('Battery-00002', 'BatteryData-ConnectorType', 'SB 300 Blue'), ..., ('Battery-0000n', 'BatteryData-ConnectorType', 'SB 9999 White');"

https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlParameterCollection_AddRange.htm

you would have to use that instead of named variables, not sure exactly how since I haven't used that interface before (and I don't program vb.net..)



Hmm I forgot about case queries. This could def be a potential. When I get home I will look into the post further. Thanks!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll