wp7——sqlite数据库操作 from:http://blog.csdn.net/wp_lijin/article/details/7370790

wp7的数据库是个头痛的问题,因为它目前不支持数据库,当然,你也可以使用微软的收费数据库或者云端,或者独立存储,不过综合下,如果你要设计一个数据管理类软件,数据库是必不可少的,下面我介绍一下Sqlite Client for Windows Phone这个数据库,如果你对这个陌生的话,先看看这个SQLite介绍

之所以选择这个数据库,是因为我对于SQL语句熟悉,而且操作过C#连接SQL,如果你也是,那么应该对下面的语句很熟悉的

下面以我做的密保通来说明:

在应用SQLite之前,要先添加两个引用

Community.CsharpSqlite.WP7

SqlLiteClient.WP7

之后添加一个命名空间:using Community.CsharpSqlite.SQLiteClient;

下面是代码部分:

 

打开(创建)数据库:

  private SqliteConnection co = null;

            co = new SqliteConnection();

            co.ConnectionString = "Version=3,uri=file:mydb.sqlite";
            co.Open();

建表:

 SqliteCommand cm = co.CreateCommand();
            cm.CommandText = "create table user(u_min text,lei integer,u_name text,u_mima text,u_bei text)";
            cm.ExecuteNonQuery();

添加数据:

  SqliteCommand cm = co.CreateCommand();
                cm.CommandText = "insert into user values(@u_min,@lei,@u_name,@u_mima,@u_bei)";
                cm.Parameters.Add("@u_min", null);
                cm.Parameters["@u_min"].Value = textBox1.Text;
                cm.Parameters.Add("@lei", null);
                cm.Parameters["@lei"].Value =textBox1.Text;
                cm.Parameters.Add("@u_name",null);
                cm.Parameters["@u_name"].Value = textBox2.Text;
                cm.Parameters.Add("@u_mima",null);
                cm.Parameters["@u_mima"].Value = passwordBox1.Password;
                cm.Parameters.Add("@u_bei",null);

                cm.Parameters["@u_bei"].Value = textBox3.Text;

                cm.ExecuteNonQuery();

 

查找数据:

public SqliteDataReader re = null;

SqliteCommand cm = co.CreateCommand();

cm.CommandText = "select * from user where lei“;
re = cm.ExecuteReader();

re.Read();

textBox3.Text=re["u_min"].ToString();

 

删除和更新类似:

  SqliteCommand cm = co.CreateCommand();
            cm.CommandText = "update user set u_min=@min,lei=@lei,u_name=@name,u_mima=@mima,u_bei=@bei where u_mima='" + mima + "'";
            cm.Parameters.Add("@min", null);
            cm.Parameters["@min"].Value = textBoxmin.Text;
            cm.Parameters.Add("@lei", null);
            cm.Parameters["@lei"].Value = no;
            cm.Parameters.Add("@name", null);
            cm.Parameters["@name"].Value = textBoxname.Text;
            cm.Parameters.Add("@mima", null);
            cm.Parameters["@mima"].Value = textBoxmima.Text;
            cm.Parameters.Add("@bei", null);
            cm.Parameters["@bei"].Value = textBoxbei.Text;
            cm.ExecuteNonQuery();

 

这里要特别说明的是,如果要在SQL语句中接查询条件(where="   ")的话,里面查询的条件东西,不能是中文汉字,不然会查询不到

大家如果仔细看了上面的代码,就会发现,其实使用Sqlite Client for Windows Phone还是很简单的,只不过网上的资源比较少,找来找去

都是那篇文章:微软WP7本地数据库之Sqlite编程技巧;而且我本人照着文章试过,没有用,还绕了好大的弯子,所以写下这个,希望大家不要

走弯路。。。。我是第一次发博客,而且接触WP7时间不长,大家有什么疑问,我尽量回答

转载于:https://www.cnblogs.com/songtzu/archive/2012/08/09/2629696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值