使用Linq为windows phone编写sql

本文关键字:编写 sql phone windows Linq 使用 | 更新日期: 2023-09-27 18:21:32

我正在尝试显示我的windows 8应用程序的历史记录,其中包括日期、时间、楼层、区域、经度和纬度。我尝试了下面的代码,但上面没有输出。

你可以在下面的链接上看到我想在我的应用程序上显示的图像。但当我运行程序时,我什么也看不到。

我有三个类用于使用linq-to-sql检索数据库并通过它显示信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;   
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.Linq;

namespace SmartParking
{
public partial class History : PhoneApplicationPage
{
    private readonly HistoryDataContext historylog; 
    public History()
    {
        InitializeComponent();
       // createDB();


    }
    public HistoryDataContext Log
    {
        get { return historylog; }
    }
    public void createDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            if (historylog.DatabaseExists() == false)
            {
                historylog.CreateDatabase();
                addDataDB();
            }
        }
    }
    public void addDataDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            HistoryDB hdb = new HistoryDB
            {
               // Date = DateTime.Today,
               // Time = DateTime.Now.TimeOfDay,
                Zone = Checkin.Zone_st,
                Floor = Checkin.Floor_st,
                location_latitude = Checkin.Latitud_do,
                location_longtitud = Checkin.Longtitude_do
            };
            historylog.history.InsertOnSubmit(hdb);
            historylog.SubmitChanges();
            GetHistoryLog();
        }
    }
    public IList<HistoryDB> GetHistoryLog()
    {
        IList<HistoryDB> HistoryList = null;
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            IQueryable<HistoryDB> query = from histoy in historylog.history select histoy;
            HistoryList = query.ToList();
        }
        return HistoryList ;
    }

}
}

下一个类是HistoryDB.cs,包含表和列

[Table]
    public class HistoryDB 
{
    [Column(CanBeNull = false)]
    public DateTime Date
    { get; set; }
    [Column(CanBeNull = false)]
    public TimeSpan Time
    { get; set; }
    [Column(CanBeNull = false)]
    public String Zone
    { get; set; }
    [Column(CanBeNull = false)]
    public String Floor
    { get; set; }
    [Column(CanBeNull = false)]
    public double location_latitude
    { get; set; }
    [Column(CanBeNull = false)]
    public double location_longtitud
    { get; set; }
}

下一个类是HistoryDataContext.cs。

public class HistoryDataContext:DataContext 
{
    public static string DBConnectionString = "Data Source=isostore:/History.sdf";
    public HistoryDataContext(string DBConnectionString)
        : base(DBConnectionString)
    {
}
   public Table<HistoryDB> history
    {
        get
        {
            return this.GetTable<HistoryDB>();
        }
    }

}

我正在尝试从NFC标签中获取信息,并将其存储在本地数据库中,然后在历史页面中检索。下面的代码是从nfc标签中读取的,但我不知道如何再次从那里保存到本地数据库中,并在历史页面中检索它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Proximity;
using NdefLibrary.Ndef;
using NdefLibraryWp.Ndef;
using Windows.Networking.Sockets; // needed for DataReader, DataWriter
using Windows.UI.Popups;
using Microsoft.Phone.UserData;
using System.Text;
using Windows.Phone.PersonalInformation;
using SmartParking.Resources;
using System.Diagnostics;

namespace SmartParking
{
public partial class Checkin : PhoneApplicationPage
{
    private ProximityDevice _device;
    private long _subscriptionIdNdef;
    public static double Latitud_do { get; set; }
    public static double Longtitude_do { get; set; }
    public static string Floor_st { get; set; }
    public static string Zone_st { get; set; }
    History store = new History();

    public Checkin()
    {
        InitializeProximityDevice();
        InitializeComponent();

    }
    private void SetLogStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (LogStatus != null) LogStatus.Text = newStatus; });
    }
    private void SetFloorStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (FloorStatus != null) FloorStatus.Text = newStatus; });
    }

    private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show(" ");
    }

    private void InitializeProximityDevice()
    {
        _device = Windows.Networking.Proximity.ProximityDevice.GetDefault();
        if (_device != null)
        {
            _subscriptionIdNdef = _device.SubscribeForMessage("NDEF", MessageReceivedHandler);
        }
    }

    private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
    {
        var rawMsg = message.Data.ToArray();
        var ndefMessage = NdefMessage.FromByteArray(rawMsg);

        ////// Loop over all records contained in the NDEF message
        foreach (NdefRecord record in ndefMessage)
        {
            if (NdefTextRecord.IsRecordType(record))
            {
                // Convert and extract URI info
                var textRecord = new NdefTextRecord(record);
                //var str = textRecord.Text;
                string[] str = textRecord.Text.Split('|');
                var latitude = str[2];
                Latitud_do = double.Parse(latitude);
                var longtitude = str[3];
                Longtitude_do = double.Parse(longtitude);
                var Floor_st = str[0];
                var Zone_st = str[1];

                SetLogStatus("Floor: " + Floor_st + " Zone: " + Zone_st );
                SetFloorStatus("Longitude: " + latitude + "   Longitude: " + longtitude);
                store.addDataDB();
            }
        }
      }
 }

}

表格的图像位于下方的链接中

https://www.dropbox.com/s/g87yta6hegjstge/Untitled.png?dl=0

使用Linq为windows phone编写sql

试试这个:

在数据库中应该有一个主键,否则它将引发异常,并且不支持TimeSpan的数据类型,因此您需要采用DateTimeString如下所示:

[Table]
    public class HistoryDB
    {
        [Column(IsPrimaryKey = true)]
        public int Id { get; set; }
        [Column(CanBeNull = false)]
        public DateTime Date
        { get; set; }
        [Column(CanBeNull = false)]
        public DateTime Time
        { get; set; }
        [Column(CanBeNull = false)]
        public String Zone
        { get; set; }
        [Column(CanBeNull = false)]
        public String Floor
        { get; set; }
        [Column(CanBeNull = false)]
        public double location_latitude
        { get; set; }
        [Column(CanBeNull = false)]
        public double location_longtitud
        { get; set; }
    }
        public void addDataDB()
        {
            using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
            {
                HistoryDB hdb = new HistoryDB
                {
                    Id = 0,
                     Date = DateTime.Today,
                    Time = DateTime.Now,
                    Zone = "Zone",
                    Floor = "Floore",
                    location_latitude = 00.00,
                    location_longtitud = 00.00
                };
                historylog.history.InsertOnSubmit(hdb);
                historylog.SubmitChanges();
                GetHistoryLog();
            }
        }

我已经实现了上面的东西,它可以正常工作