Wp7和Sql Compact

时间:2012-03-26 21:28:25

标签: c# windows-phone-7

我是c#的新手并赢得了手机7 我创建了一个简单的数据库我读了这个例子 http://f5debug.net/2012/02/26/learn-windows-phone-7-development-in-31-days-day-26-working-with-creating-a-local-database-in-wp7/ 我在主页

中打开Db
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!!!");
       }

   }

现在在主页面我创建一个按钮而不是打开另一个页面

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

现在我不知道可以从InsertData页面(InsertData.xaml.cs)访问Db,

最好的注册 安东尼奥

2 个答案:

答案 0 :(得分:1)

比你想象的更简单。 :)

var db = new SampleData.EventoDataContext();
db.MyTable.InsertOnSubmit(new MyTable() { ... });
db.Submit();
  1. “MyTable”是您在数据库中定义的表的名称。

  2. 确保定义主键,否则插入表格将失败。

  3. 您需要在{...}部分内初始化您的表格。

  4. 从表格中获取项目:

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

    这将返回SomeProp为1的所有行。您现在可以检查项目以查看该行包含的内容。

答案 1 :(得分:0)

尝试研究vici cool存储过程。从WP7应用程序中的任何数据库创建,添加和检索数据非常简单

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

相关问题