wp7——sqlite資料庫操作 from:http://blog.csdn.net/wp_lijin/article/details/7370790

來源:互聯網
上載者:User

wp7的資料庫是個頭痛的問題,因為它目前不支援資料庫,當然,你也可以使用微軟的收費資料庫或者雲端,或者隔離儲存區 (Isolated Storage),不過綜合下,如果你要設計一個資料管理類軟體,資料庫是必不可少的,下面我介紹一下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時間不長,大家有什麼疑問,我盡量回答

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.