帮我在winrt中添加sql lite [英] help me to add sql lite in winrt

查看:64
本文介绍了帮我在winrt中添加sql lite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者winrt和sql lite,我正在创建应用程序,即Customer表

我想要如何向表中添加数据,编辑数据,删除所选数据以及删除所有

请帮帮我任何一个

这里的代码可能有误请正确的代码提供给我

I am Beginner winrt and sql lite , I am creating app that Customer table
I want how to add data to the Table, edit the data, delete selected data, and delete all
Please help me Any One
Here code maybe wrong please right code provide me

using SQLite;

namespace MVVM.Models
{
    public class Customer
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Contact { get; set; }
    }
}

/////  Relay Command

/// <summary>
    /// A command whose sole purpose is to relay its functionality 
    /// to other objects by invoking delegates. 
    /// The default return value for the CanExecute method is 'true'.
    /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
    /// <see cref="CanExecute"/> is expected to return a different value.
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        /// <summary>
        /// Raised when RaiseCanExecuteChanged is called.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        /// <returns>true if this command can be executed; otherwise, false.</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        /// <summary>
        /// Executes the <see cref="RelayCommand"/> on the current command target.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        public void Execute(object parameter)
        {
            _execute();
        }

        /// <summary>
        /// Method used to raise the <see cref="CanExecuteChanged"/> event
        /// to indicate that the return value of the <see cref="CanExecute"/>
        /// method has changed.
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }


/// ViewModel Base

class ViewModelBase
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

///MainPageViewModel

class MainPageViewModel : ViewModelBase
    {
        #region Properties

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string name = string.Empty;
        public string Name
        {
            get
            { return name; }

            set
            {
                if (name == value)
                { return; }

                name = value;
                isDirty = true;
                RaisePropertyChanged("Name");
            }
        }

        private string city = string.Empty;
        public string City
        {
            get
            { return city; }

            set
            {
                if (city == value)
                { return; }

                city = value;
                isDirty = true;
                RaisePropertyChanged("City");
            }
        }

        private string contact = string.Empty;
        public string Contact
        {
            get
            { return contact; }

            set
            {
                if (contact == value)
                { return; }

                contact = value;
                isDirty = true;
                RaisePropertyChanged("Contact");
            }
        }

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }

        #endregion "Properties"

        public ObservableCollection<Customer> _customerlist { get; set; }
 public Person CustomerToAdd { get; set; }

        /// <summary>
        /// Add Customer data to the table
        /// </summary>
        public RelayCommand AddCusomerCommand { get; set; }
        private void addCustomer()
        {
        }
        /// <summary>
        /// Delete Selected Customer data from the table
        /// </summary>
        public RelayCommand DeleteSelectedCustomerCommand { get; set; }
        private void deleteSelectedCustomer()
        {

        }
        /// <summary>
        /// edit Selected Customer data from the table
        /// </summary>
        public RelayCommand EditSelectedCustomerCommand { get; set; }
        private void editSelectedCustomer()
        {

        }
        /// <summary>
        /// Delete All Customer data from the table
        /// </summary>
        public RelayCommand DeleteAllCustomerCommand { get; set; }
        private void deleteAll()
        {

        }

        public MainPageViewModel()
        {
            AddCusomerCommand = new RelayCommand(addCustomer);
            DeleteAllCustomerCommand = new RelayCommand(deleteAll);
            EditSelectedCustomerCommand = new RelayCommand(editSelectedCustomer);
            DeleteSelectedCustomerCommand = new RelayCommand(deleteSelectedCustomer);
        }


    }
/////////// App.Xaml.Cs
sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"data.db3");
            using (var db = new SQLite.SQLiteConnection(dbpath))
            {
                // Create the tables if they don't exist
                db.CreateTable<Customer>();
                db.Commit();

                db.Dispose();
                db.Close();
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MVVM.Views.Page1), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }




/// xaml Code
<Page.DataContext>
        <ViewModels:MainPageViewModel>

        </ViewModels:MainPageViewModel>
    </Page.DataContext>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding _customerlist}"

                  HorizontalAlignment="Left" Margin="44,65,0,327" Width="456">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="400" Background="Chocolate">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" FontSize="30" />
                            <TextBlock Text="," FontSize="30" />
                            <TextBlock Text="{Binding City }" FontSize="30" />
                        </StackPanel>
                        <TextBlock Text="{Binding Contact}" FontSize="30" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button Command="{Binding  AddCustomerCommand}" 

        		Content="Add person" 

        		FontSize="40" Margin="588,465,0,230"/>
        <Button Command="{Binding EditSelectedCustomerCommand}" 

        		Content="Edit" 

        		FontSize="40" Margin="865,465,0,230"/>
        <Button Command="{Binding DeleteSelectedCustomerCommand}" 

                Content="Delete" 

                FontSize="40" Margin="1037,465,0,230" />
        <Button Command="{Binding DeleteAllCustomerCommand }" 

                Content="Delete All" 

                FontSize="40" Margin="979,619,0,76" />

        <TextBlock Text="Name" FontSize="30" Margin="633,65,598,640" Height="63"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Name, Mode=TwoWay}" 

                 FontSize="30" Margin="868,62,80,640"/>
        <TextBlock Text="City " FontSize="30" Margin="633,181,551,524"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding City, Mode=TwoWay}" 

                 FontSize="30" Margin="868,181,80,525"/>
        <TextBlock Text="Age" FontSize="30" Margin="633,296,536,400"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Contact, Mode=TwoWay}" 

                 FontSize="30" Margin="868,296,80,403"/>
    </Grid>

推荐答案

这篇关于帮我在winrt中添加sql lite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆