wp7——sqlite數據庫操作

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時間不長,大家有什麼疑問,我儘量回答

 

 

 

http://blog.csdn.net/wp_lijin/article/details/7370790
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章