Heim > Datenbank > MySQL-Tutorial > 操作本地数据库

操作本地数据库

WBOY
Freigeben: 2016-06-07 15:41:39
Original
1273 Leute haben es durchsucht

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

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

Nach dem Login kopieren
 /// <summary>
    /// 课程类
    /// </summary>
   <strong><span> [Table]</span></strong> //表示类将成为一个table
    public class Course : INotifyPropertyChanged, INotifyPropertyChanging
    {
       <strong><span> [Column(IsVersion = true)] </span></strong>//table的列
        private Binary _version;

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

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

        private string _location;
       <strong><span> [Column]</span></strong>
        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));
            }
        }
    }
Nach dem Login kopieren
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();
            }
        }
       <strong><span> //必须存在,否则创建数据库报错:DataContext不存在表
        public Table<course> CourseTable
        {
            get { return this.GetTable<course>(); }
        }</course></course></span></strong>
    }
Nach dem Login kopieren

3.页面前台代码
<!--ContentPanel - 在此处放置其他内容-->
        <grid x:name="ContentPanel" grid.row="1" margin="12,0,12,0">
            <grid.rowdefinitions>
                <rowdefinition height="Auto"></rowdefinition>
                <rowdefinition height="*"></rowdefinition>
            </grid.rowdefinitions>
            <stackpanel grid.row="0" orientation="Horizontal">
                <button content="添加" click="Button_Click"></button>
                <button content="编辑" click="Button_Click_1"></button>
                <button content="删除" click="Button_Click_2"></button>
            </stackpanel>
            <listbox grid.row="1" name="CourseList">
               <strong><span> <listbox.itemtemplate></listbox.itemtemplate></span>
                   <span> <datatemplate></datatemplate></span></strong>
                        <grid>
                            <grid.columndefinitions>
                                <columndefinition width="Auto"></columndefinition>
                                <columndefinition width="Auto"></columndefinition>
                                <columndefinition width="Auto"></columndefinition>
                            </grid.columndefinitions>
                            <textblock grid.column="0" text="{Binding Id,Mode=TwoWay}"></textblock>
                            <textblock grid.column="1" text="{Binding Name,Mode=TwoWay}"></textblock>
                            <textblock grid.column="2" text="{Binding Location,Mode=TwoWay}"></textblock>
                        </grid>
                    <strong><span></span>
              <span>  </span></strong>
               
            </listbox>
        </grid>
Nach dem Login kopieren

4.页面后台代码

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

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

编辑:调用SubmitChanges()

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

 public partial class MainPage : PhoneApplicationPage
    {
        <strong><span>private ObservableCollection<course> Courses;</course></span></strong>
        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<course>
           {
            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)
            {
               <strong><span> _data.GetTable<course>().InsertOnSubmit(c);//插入数据库</course></span></strong>
            }
            <strong><span>_data.SubmitChanges();
            this.CourseList.ItemsSource = Courses;</span></strong>
           
        }
        //添加事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Course c = new Course()
            {
                Name = "客户关系管理",
                Location = "教学楼401"
            };
            Courses.Add(c);
          <strong><span>  _data.GetTable<course>().InsertOnSubmit(c);
            _data.SubmitChanges();</course></span></strong>
        }
        //编辑事件
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "网络营销");
            c.Location = "编辑教学楼";
           <span><strong> _data.SubmitChanges();</strong></span>
            
        }
        //删除事件
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "电子商务");
            Courses.Remove(c);
          <strong><span>  _data.GetTable<course>().DeleteOnSubmit(c);
            _data.SubmitChanges();</course></span></strong>
        }
    }</course>
Nach dem Login kopieren




Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage