C# 在ViewModel类中的SQl Db的可观察集合中插入值

C# 在ViewModel类中的SQl Db的可观察集合中插入值,c#,wpf,mvvm,C#,Wpf,Mvvm,我的表有两列:CourseID和CourseNAme。我要的是价值观 这两列位于可观察集合-FillCourseId中。在我看来 模范班。请帮助我,我无法填充我的可观察 使用我的数据库中的值 // ViewMOdel Class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.Col

我的表有两列:CourseID和CourseNAme。我要的是价值观 这两列位于可观察集合-FillCourseId中。在我看来 模范班。请帮助我,我无法填充我的可观察 使用我的数据库中的值

//     ViewMOdel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;


namespace MVVMDemo
{
    public class ViewModel : ViewModelBase, INotifyPropertyChanged
    {
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;
        private ObservableCollection<Student> _fillCourseId = new 
        ObservableCollection<Student>();
        static String connectionString = @"Data Source=Ramco- 
         PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated 
         Security=True;";
        SqlConnection con;
        SqlCommand cmd;
       // SqlDataAdapter adapter;
       // DataSet ds;
        //SqlDataReader reader;

        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }


        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
               // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName =dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }
        }
        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);

        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        // Property Changed Event 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

//Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVMDemo
{
      public class Student
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public string Course { get; set; }
          public string CourseID { get; set; }
//ViewMOdel类
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Input;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Data.SqlClient;
使用系统数据;
名称空间MVVMDemo
{
公共类ViewModel:ViewModelBase,INotifyPropertyChanged
{
私立学生;
私立大学学生;
私有ICommand _SubmitCommand;
私有ObservableCollection\u fillCourseId=新建
可观察收集();
静态字符串连接字符串=@“数据源=Ramco-
PC\SQLEXPRESS;初始目录=SIT\U Ramco\U DB;集成
安全性=真;“;
SqlConnection-con;
SqlCommand命令cmd;
//SqlDataAdapter;
//数据集ds;
//SqlDataReader;
公共可观测集合FillCourseId
{
获取{return\u fillCourseId;}
设置
{
_fillCourseId=值;
OnPropertyChanged(“系统状态数据”);
}
}
公立学生
{
得到
{
返回学生;
}
设置
{
_学生=价值观;
通知财产变更(“学生”);
}
}
公选学生
{
得到
{
返回学生;
}
设置
{
_学生=价值观;
通知财产变更(“学生”);
}
}
公共ICommand提交命令
{
得到
{
如果(_SubmitCommand==null)
{
_SubmitCommand=newrelaycommand(param=>this.Submit(),
无效);
}
返回_SubmitCommand;
}
}
public void GetCourseIdFromDB()
{
尝试
{
con=新的SqlConnection(connectionString);
con.Open();
cmd=新的SqlCommand(“从开发课程中选择*”,con);
SqlDataAdapter=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
适配器填充(dt);
//学生=新生();
对于(int i=0;i
我有一个包含两列的表:CourseID和CourseNAme 在我看来,可观察集合中的这两列是FillCourseId 模型类。请帮助我,我无法填充我的可观察对象
使用“我的数据库”中的值进行收集

将此添加到app.xaml.cs中即可

//     ViewMOdel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;


namespace MVVMDemo
{
    public class ViewModel : ViewModelBase, INotifyPropertyChanged
    {
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;
        private ObservableCollection<Student> _fillCourseId = new 
        ObservableCollection<Student>();
        static String connectionString = @"Data Source=Ramco- 
         PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated 
         Security=True;";
        SqlConnection con;
        SqlCommand cmd;
       // SqlDataAdapter adapter;
       // DataSet ds;
        //SqlDataReader reader;

        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }


        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
               // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName =dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }
        }
        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);

        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        // Property Changed Event 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

//Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVMDemo
{
      public class Student
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public string Course { get; set; }
          public string CourseID { get; set; }
public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var mainWindow = new Window();
            var viewModel = new ViewModel();
            mainWindow.DataContext = viewModel;
            mainWindow.Show();
            viewModel.GetCourseIdFromDB();
        }
    }
}

“我无法填充[…]是什么意思?”?你有错误吗?如果有,具体是什么错误?@germi我在我的Observable集合中添加了一个断点。对于cmd和con,它表示运行时无法计算此表达式。你的哪行代码引发了错误?你能发布错误的全文吗?@ChrisMack我在我的Observable集合中没有得到任何值。当我打开quickwatch查看“ObservableCollection FillCourseId”,并检查cmd和con的tye值。它表示“运行时无法计算此表达式”。您是否在
数据表dt
中获得任何行?