当前位置:Gxlcms > mysql > 操作本地数据库

操作本地数据库

时间:2021-07-01 10:21:17 帮助过:18人阅读

1.创建实体 注意加Table和Column特性 /// summary /// 课程类 /// /summary [Table] //表示类将成为一个table public class Course : INotifyPropertyChanged, INotifyPropertyChanging { [Column(IsVersion = true)] //table的列 private Binary _version;

1.创建实体 注意加Table和Column特性

 /// 
    /// 课程类
    /// 
    [Table] //表示类将成为一个table
    public class Course : INotifyPropertyChanged, INotifyPropertyChanging
    {
        [Column(IsVersion = true)] //table的列
        private Binary _version;

        private int _id;
        [Column(IsPrimaryKey=true,IsDbGenerated=true)] //table的列,主键,自动生成
        public int Id
        {
            get { return _id; }
            set 
            {
                if (_id != value)
                {
                    RaiseProtertyChanging("Id");
                    _id = value;
                    RaisePropertyChanged("Id");
                }
            }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    RaiseProtertyChanging("Name");
                    _name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        private string _location;
        [Column]
        public string Location
        {
            get { return _location; }
            set
            {
                if (_location != value)
                {
                    RaiseProtertyChanging("Location");
                    _location = value;
                    RaisePropertyChanged("Location");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void RaiseProtertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }
2.创建类继承DataContext,而且封装this.GetTable()方法,否则创建数据库时报错
 public class MyDataContext : DataContext
    {
        //连接字符窜
        public const string ConnectionString = "Data Source=isostore:/MyDb.sdf";

        //构造函数
        public MyDataContext()
            : base(ConnectionString)
        {
            if (!this.DatabaseExists())
            {
                //创建数据库
                this.CreateDatabase();
            }
        }
        //必须存在,否则创建数据库报错:DataContext不存在表
        public Table CourseTable
        {
            get { return this.GetTable(); }
        }
    }

3.页面前台代码

        
            
                
                
            
            
                

4.页面后台代码

添加:调用GetTable().InsertOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。

删除: GetTable().DeleteOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。

编辑:调用SubmitChanges()

页面绑定的数据源必须是ObservableCollection类型

 public partial class MainPage : PhoneApplicationPage
    {
        private ObservableCollection Courses;
        private DataContext _data;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            //创建数据库
            //CreateDatabase();
            _data = new MyDataContext();
            //初始化数据
            InitData();
        }

        private void CreateDatabase()
        {
           _data = new MyDataContext();
            if (!_data.DatabaseExists())
            {
                _data.CreateDatabase();
            }
        }

        private void InitData()
        {
            Courses = new ObservableCollection
           {
            new Course{Name="电子商务",Location="教学楼101"},
            new Course{Name="心理学",Location="教学楼101"},
            new Course{Name="高等数学",Location="教学楼101"},
            new Course{Name="网络营销",Location="教学楼101"},
           };
            foreach (var c in Courses)
            {
                _data.GetTable().InsertOnSubmit(c);//插入数据库
            }
            _data.SubmitChanges();
            this.CourseList.ItemsSource = Courses;
           
        }
        //添加事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Course c = new Course()
            {
                Name = "客户关系管理",
                Location = "教学楼401"
            };
            Courses.Add(c);
            _data.GetTable().InsertOnSubmit(c);
            _data.SubmitChanges();
        }
        //编辑事件
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "网络营销");
            c.Location = "编辑教学楼";
            _data.SubmitChanges();
            
        }
        //删除事件
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "电子商务");
            Courses.Remove(c);
            _data.GetTable().DeleteOnSubmit(c);
            _data.SubmitChanges();
        }
    }

人气教程排行