[英]What type of array should I use for a database of information about the user? vb.net
So I'm trying to make a system where a person can input data about different people and I want to store it in a textile to search and sort the data, It would contain first name, surname, location, phone number.所以我试图建立一个系统,一个人可以输入关于不同人的数据,我想把它存储在纺织品中以搜索和排序数据,它会包含名字、姓氏、位置、电话号码。
If I were to use a 2d array how would I go about inputting the data through a loop instead of just writing it out myself.如果我要使用二维数组,我 go 如何通过循环输入数据而不是自己写出来。
Or I could use one dimensional arrays and store each type of data, first name, surname etc in separate text files but I am not sure if that would work/be efficient.或者我可以使用一维 arrays 并将每种类型的数据、名字、姓氏等存储在单独的文本文件中,但我不确定这是否可行/是否有效。
If you stored the data as JSON, it would be easier to maintain.如果将数据存储为 JSON,则更易于维护。
You would create a class to represent the data, read in the file, parse the contents to a collection of your class, and then use LINQ to do your filtering.您将创建一个 class 来表示数据,读取文件,将内容解析为 class 的集合,然后使用 LINQ 进行过滤。
This would be efficient enough if you aren't doing any inserts/updates, in which case I would recommend moving to a database to handle these.如果您不进行任何插入/更新,这将足够有效,在这种情况下,我建议您移至数据库来处理这些操作。 You could still use the same approach and just serialize the JSON collection back to the file, but you would start to see some significant slow down of the write operation pretty quickly.
您仍然可以使用相同的方法,只需将 JSON 集合序列化回文件,但您会很快发现写操作速度明显变慢。
Here is an example of defining the class:以下是定义 class 的示例:
Public Class Person
<JsonProperty("firstName")>
Public Property FirstName As String
<JsonProperty("surname")>
Public Property Surname As String
<JsonProperty("location")>
Public Property Location As String
<JsonProperty("phoneNumber")>
Public Property PhoneNumber As String
End Class
Here is an example of reading a text file and parsing it to a collection of your class:以下是读取文本文件并将其解析为您的 class 集合的示例:
Public Function GetPeople(path As String) As IEnumerable(Of Person)
Dim literal = IO.File.ReadAllText(path)
Dim people = JsonConverter.DeserializeObject(Of IEnumerable(Of Person))(literal)
Return people
End Function
Here is an example of adding people to the collection and serializing it back to the file:这是将人员添加到集合并将其序列化回文件的示例:
Public Function AddPerson(path As String, record As Person) As IEnumerable(Of Person)
Dim people = GetPeople(path).ToList()
people.Add(record)
IO.File.WriteAllText(path, people.ToString())
Return people
End Function
Keep in mind you'd want to add exception handling and business logic to check that the state is what you would expect it to be.请记住,您需要添加异常处理和业务逻辑来检查 state 是否符合您的预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.