C#.net 3.5 Compact SqlCe

时间:2014-02-07 20:33:29

标签: c# sql database .net-3.5 compact-framework

好的,所以我正在尝试为我的HP Ipaq211编写一个程序,我可以在工作中使用它(我是服务器)接受命令,而不是使用纸张。我已经走得很远,并决定最好使用数据库来保存完整的菜单信息。我创建了一个饮料数据库,以4列{ID,Item,Price,Options}开头,其中ID是主键。

我创建了一些混合物,允许我将数据读入一个对象,然后创建这些所述对象的列表,但是它们都执行得非常慢(Ipaq上为4秒)。我已经在编程方面教会了我所知道的一切,所以请耐心等待,这是我的尝试之一(虽然有效但很慢,我需要它更快地工作!)

    public class _itemObject
    {

    public _itemObject()
    {

        ID = 0;
        _ioName = "";
        _ioPrice = "";
        _ioOptions = -1;

    }
        public _itemObject(int _next, string Tbl_Name)
        {
        try
        {

            string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
                    "\\TestDatabase.sdf;Persist Security Info=True";
            SqlCeConnection _connection = new SqlCeConnection(conSTR);


            SqlCeCommand _cmd = new SqlCeCommand("select ID from " + Tbl_Name + " where ID ='" + _next.ToString() + "'", _connection);
            SqlCeCommand _cmd2 = new SqlCeCommand("select * from " + Tbl_Name + " where ID ='" + _next.ToString() + "'", _connection);
            SqlCeCommand _cmd3 = new SqlCeCommand("select price from " + Tbl_Name + " where ID ='" + _next.ToString() + "'", _connection);
            SqlCeCommand _cmd4 = new SqlCeCommand("select special from " + Tbl_Name + " where ID ='" + _next.ToString() + "'", _connection);



            _connection.Open();
            if (_cmd.ExecuteScalar() != null)
            {

                ID = (Convert.ToInt32(_cmd.ExecuteScalar().ToString()));
                _ioName = _cmd2.ExecuteScalar().ToString();
                _ioPrice = _cmd3.ExecuteScalar().ToString();
                _ioOptions = (Convert.ToInt32(_cmd4.ExecuteScalar().ToString()));
            }
            else
            {

            }
            _connection.Close();
        }

        finally
        {

        }
        }

然后将此对象添加到List<_itemObject>,我从中加载任何所需的数据。

我知道这很丑,但如果有人为我上课,我会很感激:)

1 个答案:

答案 0 :(得分:1)

答案可能与您的最终目标不同。

a)为什么需要使用4个sql命令?一个命令应该可以获取所有信息。例如:

SELECT * FROM table_name;

将在SqlCeDataReader.ExecuteReader()调用中一次报告所有数据,您可以迭代这些数据以填充列表。

b)如果以后没有调用SQL服务器(用于远程访问/同步等),如果没有太多数据记录,您可以考虑切换到另一个数据存储(即xml(慢速)或二进制文件)。 / p>

如果您需要更多帮助,请提供更多详细信息。

在stackoverflow中还有一些SQLCE示例:Local database, I need some examples和其他(使用搜索)。

好的,从您的评论中我发现您有一些问题可以开始使用?!

http://www.codeproject.com/Articles/310378/A-Restaurant-and-Waiter-helper-app-in-WPF-and-Wind,您将找到完整的POS解决方案。您可以更改服务员的代码以使用本地数据库。

......稍后可能会添加一些简单的例子......