zoukankan      html  css  js  c++  java
  • 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时间不长,大家有什么疑问,我尽量回答

  • 相关阅读:
    Update操作一定是先Delete再Insert吗?
    asp.net中的并发控制
    asp.net2.0下利用javascript实现treeview中的checkbox全选
    Json
    SQL Server2005 事务隔离级别
    设计概念模型
    前沿视频教程
    PowerDesigner使用
    表单 数字字符验证正则
    CharIndex對比Replace
  • 原文地址:https://www.cnblogs.com/songtzu/p/2629696.html
Copyright © 2011-2022 走看看