Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#, SQL Server.
How to save multi strings in the database(in one cell) and retrieve them to winform in different textboxes?

What I have tried:

can i seperate them by sign like($$) to help me distribute to different text boxes programmatically?
Posted
Updated 16-Feb-17 5:41am
Comments
[no name] 16-Feb-17 10:51am    
Well you start off with a really bad design....

"can i seperate them by sign", yes you can.... Did you actually try it?
XRushdy 16-Feb-17 11:00am    
the situation is i got a clinic project, every patient has a prescription must be in his record, this prescription will contain multi strings(medicin name per line), now i'm looking for good way to do this
[no name] 16-Feb-17 11:31am    
Asking people for advice on a project they know nothing about, isn't likely to help you. Personally, I would have a prescription table that I would link to from my patient table.
XRushdy 16-Feb-17 10:55am    
yes, but u're right, i feel that it will be bad design, so i'm looking for another idea
XRushdy 16-Feb-17 11:02am    
is there a way to save those medicin names in prescription column in patient table?

1 solution

Don't do it. You can, it's not difficult - beginners do it a lot with a column containing comma separated values. But ... it's a poor idea because while it's easy to insert and trivial to code, it gives huge headaches down the line, by which time it's too late to change it and you have to live with the problems.
For example, suppose you have three values separated by '|': "John Smith|12 Green Lane|Morcombe" That works fine and it's easy to use Split in C# to separate them. But what happens when he moves to Peach Pie Street? To change it, you have to pull the row from the DB, split it, change the string, re-join it, and push it back to the DB.

Instead of that, create a second table:
ID        CustName     CustAddress     CustTown
1234    John Smith   12 Green Lane     Morcombe

You then include a Foreign Key column in your main table with refers to this by the ID column.
When you want to change the address, you just issue an UPDATE to the DB referencing the correct ID, and it's done.
And when you want to get the info, you use a nice simple SELECT with a JOIN and you get the relevant data in separate columns, ready to be bound to the textboxes directly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900