Linq to sql 无法在windows phone应用程序中向本地数据库添加值
我正在尝试为Windows Phone 8创建数据库应用程序。当我试图在DB的一个表中添加一个新的表项时,我会遇到如下错误Linq to sql 无法在windows phone应用程序中向本地数据库添加值,linq-to-sql,windows-phone-8,.net,local-database,Linq To Sql,Windows Phone 8,.net,Local Database,我正在尝试为Windows Phone 8创建数据库应用程序。当我试图在DB的一个表中添加一个新的表项时,我会遇到如下错误 {System.InvalidCastException: Could not convert from type 'System.Byte[]' to type 'System.String'. at System.Data.Linq.DBConvert.ChangeType(Object value, Type type) at System.Dat
{System.InvalidCastException: Could not convert from type 'System.Byte[]' to type 'System.String'.
at System.Data.Linq.DBConvert.ChangeType(Object value, Type type)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetAutoSync(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetInsert(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at CygnusModel.ViewModel.CygnusViewModel.AddNewSem(CygSem NewCygSem)
at Cygnus.AddSemView.Save_sem(Object sender, EventArgs e)
at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args)
at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent()
at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand, Boolean isButton)
at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand, Boolean isButton)}
这是我使用的代码
if (CygSemName.Text.Length > 0)
{
CygSem NewCygSem = new CygSem
{
SemName = (string)CygSemName.Text,
SemStart = (DateTime)CygSemStart.Value,
SemEnd = (DateTime)CygSemEnd.Value
};
App.ViewModel.AddNewSem(NewCygSem);
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
谁能帮我一下吗?请记住,我是c#和Windows Phone开发的新手
视图模型:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
//Includes DataModel Classes
using CygnusModel.Model;
namespace CygnusModel.ViewModel
{
public class CygnusViewModel : INotifyPropertyChanged
{
private CygDataContext CygDb;
//Class Constructor Creating the connection between Model & View
public CygnusViewModel(string CygDataConnectionString)
{
CygDb = new CygDataContext(CygDataConnectionString);
}
//Save changing Method
private void CygSaveChanges()
{
CygDb.SubmitChanges();
}
//Contains Change tracking INotify Events
#region INotifies
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
//All The Semesters
private ObservableCollection<CygSem> _allSems;
public ObservableCollection<CygSem> AllSems
{
get
{
return _allSems;
}
set
{
_allSems = value;
NotifyPropertyChanged("AllSems");
}
}
//All The Subjects
private ObservableCollection<CygSubs> _allSubs;
public ObservableCollection<CygSubs> AllSubs
{
get
{
return _allSubs;
}
set
{
_allSubs = value;
NotifyPropertyChanged("AllSubs");
}
}
//All The Lectures
private ObservableCollection<CygLect> _allLects;
public ObservableCollection<CygLect> AllLects
{
get
{
return _allLects;
}
set
{
_allLects = value;
NotifyPropertyChanged("AllLects");
}
}
//All The Exams
private ObservableCollection<CygExam> _allExam;
public ObservableCollection<CygExam> AllExam
{
get
{
return _allExam;
}
set
{
_allExam = value;
NotifyPropertyChanged("AllExam");
}
}
//All The Instructors
private ObservableCollection<CygInst> _allInsts;
public ObservableCollection<CygInst> AllInsts
{
get
{
return _allInsts;
}
set
{
_allInsts = value;
NotifyPropertyChanged("AllInsts");
}
}
//All The Holidays
private ObservableCollection<CygHoli> _allHolis;
public ObservableCollection<CygHoli> AllHolis
{
get
{
return _allHolis;
}
set
{
_allHolis = value;
NotifyPropertyChanged("AllHolis");
}
}
//All the lectures of today
private ObservableCollection<CygLect> _todayLect;
public ObservableCollection<CygLect> TodayLect
{
get
{
return _todayLect;
}
set
{
_todayLect = value;
NotifyPropertyChanged("TodayLect");
}
}
//All the Exams of today
private ObservableCollection<CygExam> _todayExam;
public ObservableCollection<CygExam> TodayExam
{
get
{
return _todayExam;
}
set
{
_todayExam = value;
NotifyPropertyChanged("TodayExam");
}
}
// Query database and load the all the data
public void LoadAllAvailables()
{
}
//Query DB and load all the semester
public void LoadAllSems()
{
var Sems = from CygSem semes in CygDb.Semesters select semes;
AllSems = new ObservableCollection<CygSem>(Sems);
}
//Query DB and load all Holidays
public void LoadAllHolis()
{
var Holis = from CygHoli holid in CygDb.Holidays select holid;
AllHolis = new ObservableCollection<CygHoli>(Holis);
}
//Query DB and load Subjects based on Semester
public void LoadSemBasedSubs()
{
}
//Query DB and load all the Lectures and exams per day
public void LoadForTheDay()
{
}
//Query the DB and load Lectures and Exams per Subject
public void LoadForTheSub()
{
}
//Add New Sem
public void AddNewSem(CygSem NewCygSem)
{
CygDb.Semesters.InsertOnSubmit(NewCygSem);
CygDb.SubmitChanges();
AllSems.Add(NewCygSem);
}
//Add New Holiday
public void AddNewHoli(CygHoli NewCygHoli)
{
CygDb.Holidays.InsertOnSubmit(NewCygHoli);
CygDb.SubmitChanges();
}
}
}
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
//包括数据模型类
使用CygnusModel.Model;
命名空间CygnusModel.ViewModel
{
公共类CygnusViewModel:INotifyPropertyChanged
{
私人CygDataContext CygDb;
//类构造函数创建模型和视图之间的连接
公共CygnusViewModel(字符串CygDataConnectionString)
{
CygDb=新的CygDataContext(CygDataConnectionString);
}
//保存更改方法
私有无效CygSaveChanges()
{
CygDb.SubmitChanges();
}
//包含更改跟踪INotify事件
#区域索引
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
//所有学期
私人可观察收集(所有SEM);;
公共可观测集合所有SEM
{
得到
{
返回所有sems;
}
设置
{
_allSems=值;
通知财产变更(“所有SEM”);
}
}
//所有科目
私人可观察收集(allSubs);;
公共可观测集合所有子集合
{
得到
{
返回_allSubs;
}
设置
{
_allSubs=值;
NotifyPropertyChanged(“AllSubs”);
}
}
//所有的讲座
私人可观察收集选择;
公众可观察收集所有选择
{
得到
{
返回所有选择;
}
设置
{
_allLects=值;
NotifyPropertyChanged(“AllLects”);
}
}
//所有的考试
私人可观察收集(allExam);;
公众可观察收集的阿莱克斯姆
{
得到
{
返回_allExam;
}
设置
{
_allExam=值;
NotifyPropertyChanged(“AllExam”);
}
}
//所有教员
私人可观测收集装置;
公共可观测收集系统
{
得到
{
返回_allInsts;
}
设置
{
_allInsts=值;
NotifyPropertyChanged(“AllInsts”);
}
}
//所有的假期
私人可观测集合(allHolis);;
公共可观测集合AllHolis
{
得到
{
返回_allHolis;
}
设置
{
_allHolis=价值;
通知财产变更(“AllHolis”);
}
}
//今天所有的讲座
私人可观察收集(今日选择);;
今天的公众观察收集
{
得到
{
今天返回选择;
}
设置
{
_今天选择=值;
NotifyPropertyChanged(“今日选择”);
}
}
//今天所有的考试
私人可观测采集_todayExam;
今天上午的公众观察收集
{
得到
{
返回_todayExam;
}
设置
{
_todayExam=值;
NotifyPropertyChanged(“TodayExam”);
}
}
//查询数据库并加载所有数据
公共无效加载所有可用项()
{
}
//查询数据库并加载整个学期
公共void LoadAllSems()
{
var Sems=来自CygDb中的CygSem学期。学期选择学期;
AllSems=新的可观测集合(Sems);
}
//查询数据库并加载所有假日
公共空间
{
var Holis=来自CygDb中的CygHoli holid。假日选择holid;
AllHolis=新的可观测集合(Holis);
}
//基于学期查询数据库并加载科目
公共void LoadSemBasedSubs()
{
}
//查询数据库并加载每天的所有讲座和考试
公共作废日装载量()
{
}
//查询数据库并加载每个主题的讲座和考试
sub()的公共无效加载
{
}
//添加新的扫描电镜
公共无效AddNewSem(CygSem NewCygSem)
{
CygDb。学期。InsertOnSubmit(新CYGSEM);
CygDb.SubmitChanges();
AllSems.Add(NewCygSem);
}
//增加新假期
公共无效AddNewHoli(CygHoli-NewCygHoli)
{
CygDb.Holidays.InsertOnSubmit(新西里);
CygDb.SubmitChanges();
}
}
}
型号:
//Semester Class
[Table]
public class CygSem : INotifyPropertyChanging, INotifyPropertyChanged
{
//_version variable. Binary Datatype
[Column(IsVersion = true)]
private string _version;
//Contains Change tracking INotify Events
#region INotifies
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangingEventHandler PropertyChanging;
private void NotifyPropertyChanging(string PropertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(PropertyName));
}
}
#endregion
//Semester ID Variable
private int _semID;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity",
CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int SemID
{
get
{
return _semID;
}
set
{
NotifyPropertyChanging("SemID");
_semID = value;
NotifyPropertyChanged("SemID");
}
}
////Semester Name
//private string _semName;
//[Column]
//public string SemName
//{
// get
// {
// return _semName.ToString();
// }
// set
// {
// NotifyPropertyChanging("SemName");
// _semName = value.ToString();
// NotifyPropertyChanged("SemName");
// }
//}
//Semester Start Date
private DateTime _semStart;
[Column]
public DateTime SemStart
{
get
{
return _semStart;
}
set
{
NotifyPropertyChanging("SemStart");
_semStart = value;
NotifyPropertyChanged("SemStart");
}
}
//Semester End Date
private DateTime _semEnd;
[Column]
public DateTime SemEnd
{
get
{
return _semEnd;
}
set
{
NotifyPropertyChanging("SemEnd");
_semEnd = value;
NotifyPropertyChanged("SemEnd");
}
}
#region Subject Set
// Define the entity set for the collection side of the relationship.
private EntitySet<CygSubs> _subs;
[Association(Storage = "_subs", OtherKey = "_cygSemID", ThisKey = "SemID")]
public EntitySet<CygSubs> Subs
{
get
{
return this._subs;
}
set
{
this._subs.Assign(value);
}
}
// Assign handlers for the add and remove operations, respectively.
public CygSem()
{
_subs = new EntitySet<CygSubs>(
new Action<CygSubs>(this.attach_sem),
new Action<CygSubs>(this.dettach_sem)
);
}
// Called during an add operation
private void attach_sem(CygSubs _subj)
{
NotifyPropertyChanging("CygSubs");
_subj.Semester = this;
}
// Called during a remove operation
private void dettach_sem(CygSubs _subj)
{
NotifyPropertyChanging("CygSubs");
_subj.Semester = null;
}
#endregion
}
//学期班
[附表]
公共类CygSem:INotifyPropertyChanging,INotifyPropertyChanged
{
//_版本变量。二进制数据类型
[列(IsVersion=true)]
私有字符串\u版本;
//包含更改跟踪INotify事件
#区域索引
公共事件属性更改事件处理程序属性更改;
私有资产更改(strin)
[Database]
public class CygDataContext : DataContext
{
//Pass The Connection String to the base class.
public CygDataContext(string ConnectionString)
: base(ConnectionString)
{ }
//Specify table Semesters
public Table<CygSem> Semesters;
//Specify table Subjects
public Table<CygSubs> Subjects;
//Specify table Lecture
public Table<CygLect> Lectures;
//Specify table Exams
public Table<CygExam> Exams;
//Specify table Instructors
public Table<CygInst> Insts;
//Specify table Holidays
public Table<CygHoli> Holidays;
}