如何在数据库中保存多个字符串(在一个单元格中) [英] How to save multi strings in the database(in one cell)

查看:132
本文介绍了如何在数据库中保存多个字符串(在一个单元格中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,SQL Server。

如何在数据库中保存多个字符串(在一个单元格中)并在不同的文本框中将它们检索到winform?



我尝试了什么:



我可以用($$)这样的符号分隔它们,以帮助我分发到不同的文本以编程方式?

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?

推荐答案

)帮助我以编程方式分发到不同的文本框?
) to help me distribute to different text boxes programmatically?


不要这样做。你可以,这并不困难 - 初学者用包含逗号分隔值的列做了很多。但是......这是一个糟糕的主意,因为虽然它很容易插入并且很容易编写代码,但它会给网络带来巨大的麻烦,到时候为时已经太晚了,无法改变它,你必须忍受这些问题。

例如,假设你有三个以'|'分隔的值:John Smith | 12 Green Lane | Morcombe这很好用,很容易在C#中使用Split来分隔它们。但是当他搬到Peach Pie Street时会发生什么?要更改它,您必须从数据库中提取行,拆分它,更改字符串,重新连接它,然后将其推回到数据库。



而不是创建第二个表:

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



然后在主表中包含一个外键列,并通过ID列引用它。

你想改变地址,你只需要向引用正确ID的数据库发出UPDATE,就完成了。

当你想要获取信息时,你可以使用一个很好的简单SELECT和一个JOIN并且您可以在单独的列中获取相关数据,以便直接绑定到文本框。


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.


这篇关于如何在数据库中保存多个字符串(在一个单元格中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆