简体   繁体   中英

Wp7 and Sql Compact

i am newbie of c# and win phone 7 i create a simple database i read this example http://f5debug.net/2012/02/26/learn-windows-phone-7-development-in-31-days-day-26-working-with-creating-a-local-database-in-wp7/ i open Db in Mainpage

public partial class MainPage : PhoneApplicationPage
{
    // short connection string format

    private const string strConnectionString = @"isostore:/ManutenzioneDB.sdf";
    // Costruttore
    public MainPage()
    {
        InitializeComponent();

   using (SampleData.EventoDataContext Empdb = new SampleData.EventoDataContext(strConnectionString)) 
   {
       // se il db non esiste creo il db
       if (Empdb.DatabaseExists() == false)
       {
           Empdb.CreateDatabase();
      //     MessageBox.Show("Employee Database Created Successfully!!!");
       }

   }

now in Main page i create a button than open an other page

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/InsertData.xaml", UriKind.Relative));
    }

now i don't know can access to Db from InsertData page (InsertData.xaml.cs),

best regads Antonio

Simpler than you think. :)

var db = new SampleData.EventoDataContext();
db.MyTable.InsertOnSubmit(new MyTable() { ... });
db.Submit();
  1. "MyTable" is the name of the table you defined inside the database.

  2. Make sure you define a primary key, or inserting into the table will fail.

  3. You will need to initialize your table inside the {...} part.

To get items from the table:

foreach (var item in db.MyTable.Where(x => x.SomeProp == 1))
{
//…
}

This will return all the rows where SomeProp is 1. You can now inspect item to see what the row contains.

Try to study the vici cool stored procedure. It is very very simple to create, add, and retrieve data from any db in WP7 applications

http://viciproject.com/wiki/projects/coolstorage/home

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM