I’m using C# to input and output multi 2d arrays into SQL database.
The 2D array is something like :
Pt[100,50]={0.3,0.2,0.1,...,0.8;
0.2,0.5,0.5,...,0.1;
. .
. .
. .
0.1,0.6,0.5,...,0.2}
I know to design a (index_x,index_y) table in SQL and use two loops on C# to get it done.
Is there an better way to input and output the 2D array more efficiently?
Any examples would be great!
By executing a separate SQL statement at each point, you are doing as many database round-trips as there are points to insert. Each round-trip incurs a communication latency (especially if the database is not local) as well as some bookkeeping costs at the level of the DBMS.
The crude and not very scalable, but database-agnostic way to perform multiple inserts within only one database round-trip is to simply pack multiple INSERT statements into a single
DbCommandobject.Assuming your table looks similar to this (use your DBMS-specific types as appropriate)…
…here is one way to do it:
There are more efficient, but DBMS-specific solutions, such as: