开发者

How to use the class SqlCeConnection in the Window Phone 7 application?

I am developing window phone 7 application. I am new to the Window Phone 7 application development. I want to use the SQL server compact edition database in my application for database connectivity. So in my application I have created the database by right clicking the 'D开发者_开发知识库ata Connection' & Choosing the 'Add Connection' in Server Explorer. After that I have created the database by using the Create button in the opened window.Then I have tested my connection. It was successful. I have added that created database file in my application by right clicking on the Project Name & choosing the add existing item. The database file was added as the 'ExpenseManagerDB.sdf' I have created the tables for this database by using the Server Explorer. Now I want to programmatically do the database connectivity for my application. So I am using the following code

namespace ExpenseMgrMobAppl.Category
{
    public partial class Category : PhoneApplicationPage
    {
        private void Submitbutton_Click(object sender, RoutedEventArgs e)
        {
            string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\ExpenseManagerDB.sdf;Persist Security Info=False";
            SqlCeConnection connection = new SqlCeConnection(conSTR);
            SqlCeCommand cmd = new SqlCeCommand("select category_name form category where category_id=1", connection);
            connection.Open();
            textBlock1.Text = cmd.ExecuteScalar.ToString();
            connection.Close();
        }
    }
}

But I am not finding any Namespace/Assembly for the SqlCeConnection class. I have also tried to add reference to the dll 'System.Data.SqlServerCE.dll' in my application. But the reference could not be added. What should I need do for database connectivity ? What I need to do to use the SqlCeConnection class in my application ? I am looking for thing from which I can use the SqlCeConnection class in my application ? Can you please provide me any solution or link through which I can resolve the above issue ? If I am doing anything wrong in above case then please guide me.


SQL CE is not available in this release of the tools.

The alternatives are storage behind a web service, 3rd party db sources like Sterling or local storage in files using for example XML and LINQ.


As Mick N says there are some 3rd party databases available for the phone. Have a look at

  • Perst (Commercial, but I believe free for non-commercial use)
  • Sterling
  • Windows Phone 7 Database

However my advice would be to think carefully about whether you really need a full relational database in your app on the phone. If you are just using it to store and retrieve data, binary serialized files are extremely efficient and lightweight.

Remember the phone is a very resource constrained environment. Whereas on the desktop we would immediately turn to a database for most data storage requirements because it makes our life easier. On the phone we need to be more careful and optimize for the environment - even if that makes our lives as programmers a little harder. A full database shouldn't necessarily be the first option when thinking about storing data.

That said if you have a data requirement that you feel warrants a full relational database then you should absolutely use one of the aforementioned DBs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜