在SQL数据库中存储ArrayList [英] Store ArrayList in SQL Database

查看:245
本文介绍了在SQL数据库中存储ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList:



I have an ArrayList:

ArrayList arrayc = new ArrayList();
     arrayc.Insert(0,"asd");
     arrayc.Insert(1,"wet");
     arrayc.Insert(2,"wertWER");





我想将它存储在数据库中。我怎样才能实现这一目标?



I want to store it in a database. How can I achieve this?

推荐答案

有两种方法:将ArrayList中的每个项目作为数据库中的单独行(推荐)或序列化您的ArrayList并存储结果流作为二进制数据。



我会做前者:后者过分依赖于程序的实现。



另外,请尽量避免 ArrayList :使用typesafe版本 List< T> 。它通过限制可以放入列表的内容来提高可读性,并且在您取出数据时不需要检查和转换。
There are two ways: make each item in your ArrayList a separate row in a database (recommended) or serialize your ArrayList and store the resulting stream as binary data.

I would do the former: the later relies too much on the implementation of your program.

In addition, please try to avoid ArrayList: use the typesafe version List<T> instead. It promotes readability by restricting what can be put into the list, and does not require checking and casting when you take data out.






考虑创建一个SQL Compact Edition数据库,该数据库将数据库存储在一个文件中,但允许通过像实体框架这样的API进行标准数据库访问。查看此文章:



http:// technet.microsoft.com/en-us/library/cc835495.aspx [ ^ ]



这样一个SQL CE数据库随你的应用程序一起分发。



使用Entity Framework创建模型(即表结构)并从中创建数据库。然后,您可以使用创建的类将数据添加到数据库或从中查询数据。
Hi,

Consider creating an SQL Compact Edition database, which will store the database in a file but allow standard database access through an API like the entity framework. See this article:

http://technet.microsoft.com/en-us/library/cc835495.aspx[^]

This way a SQL CE database is distributed with your application.

Use the Entity Framework to create a model (i.e. the table structure) and create the database from this. You can then use the classes created to add data to your database or query data from it.


我认为另一种方法很简单,就是将项目附加到单个字符串之间使用某些符号项目( - 或_或&等)并将其存储在数据库中。在阅读它时,您可以使用一行代码将其解析为ArrayList。我使用它是因为有时候,我不知道ArrayList中有多少项可以准备列。 (确保使用您确定不会出现在任何物品中的符号。)
Another way I think is simple, is by appending the items to a single string using some symbol between the items(- or _ or &, etc) and store it in the database. While reading it, you can parse it to an ArrayList with a single line of code. I use this because sometimes, I don't know how many items there will be in the ArrayList to prepare columns. (Make sure you use a symbol that you're sure won't be present in any of your items).


这篇关于在SQL数据库中存储ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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