I’m doing a program in C# (windows forms) and SQL-Server where I need to take the strings from two columns and put them into an array. I will use that array later in order to implement autocomplete to some textBoxes. The rules are:
- The array should be able to hold whatever the amount of strings I
have in the columns, this amount may exceed 500 strings and is variable. - I will need the distinct values from the columns, no duplicates.
I think I’m supposed to use “UNION” for putting together all the strings from the two columns and SqlDataReader with a “while” cycle when putting the strings into the array.
Here is an example table, use it to explain it to me:
----------------------------
| name | surname |
----------------------------
| John | Jackson |
----------------------------
| Michael | Jones |
----------------------------
| Amanda | Lopez |
----------------------------
| Christina | Lopez |
----------------------------
So how would the query look like and how would can I put the results into an array?
If you want a concatenate use this:
If you dont want a concatenate you can do this:
A union will work as long as
Col1andCol2have the same datatype. Once you have the data you can bring it back to the client side (via a sproc) into a dataset, datatable, or sqldatareader.The rest should be simple, take the data and store it in an array of some sort.
Either through some sort of loop
Copy paste this in sql server management studio
SELECT col1 FROM #test
UNION
SELECT col2 FROM #test
The result is: