使用 Linq To Sql 保存到 .Net 中的 SDF(本地数据库文件)
您好,我正在尝试使用 LinqToSql 对象将一些数据保存到我的 .sdf 文件中。 我有一个如下表 -
PersonId int not null pk
forename nvarchar(4000)
surname nvarchar(4000)
Birthdate DateTime
IsMale bit
Biography nvarchar(4000)
我将一个值插入到除 PersonId 列之外的所有字段中(设置为身份、增量)。
的 C# 代码
myLinqToSqlObject.Persons.InsertOnSubmit(thisPerson);
我正在使用then
thisDataContext.SubmitChanges();
,并且收到“调用目标已引发异常”的异常。 以及“Varchar”的内部异常
有人知道我做错了什么吗?
谢谢
编辑 - 显示填充 thisPerson 的代码
thisPerson.Forename = thisDlg.Forename;
thisPerson.Surname = thisDlg.Surname;
thisPerson.Biography = thisDlg.Biography;
thisPerson.IsMale = thisDlg.IsMale;
thisPerson.BirthDate = thisDlg.BirthDate;
,其中所有字段都返回一个值(无空值)
HI, I am trying to save some data to my .sdf file using a LinqToSql object.
I have a table as follows -
PersonId int not null pk
forename nvarchar(4000)
surname nvarchar(4000)
Birthdate DateTime
IsMale bit
Biography nvarchar(4000)
I am inserting a value into all fields apart from the PersonId column (set to identity, increment).
I am using the C# code of
myLinqToSqlObject.Persons.InsertOnSubmit(thisPerson);
then
thisDataContext.SubmitChanges();
and i get the exception of "Exception has been thrown by the target of an invocation."
and an inner exception of just "Varchar"
Does anyone know what i'm doing wrong?
Thanks
EDIT - Showing code to populate thisPerson
thisPerson.Forename = thisDlg.Forename;
thisPerson.Surname = thisDlg.Surname;
thisPerson.Biography = thisDlg.Biography;
thisPerson.IsMale = thisDlg.IsMale;
thisPerson.BirthDate = thisDlg.BirthDate;
Where all fields return a value (no null values)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DataContext 有一个 Log 属性;如果你给它分配一个 StreamWriter 来写入文件,或者附加 Console.Out,它将写入上下文生成的 SQL,并且你可以更好地调试查询,然后可能......
另外,要弄清楚发生了什么,在提交更改之前,调用 context.GetChangeSet(),并使用即时窗口和 VS 调试来查看此集合中驻留的内容(它正在执行插入、更新和删除)。
这将帮助您更快地找到答案...
The DataContext has a Log property; if you assign a StreamWriter to it to write to a file, or attached Console.Out, it will write the SQL generated by the context, and you can better debug the query then potentially...
Also, to figure out what's going on, before submitting the changes, call context.GetChangeSet(),and use the immediate window and VS debugging to see what resides within this collection (it has the inserts, updates, and deletes being performed).
That will help you get to the answer quicker...
要使用 Visual Studio 2010 将 SDF 文件创建为 DBML,请在 Visual Studio 2010 命令提示符中写入以下行,然后输入并执行它。
此 SDF 和 DBML 文件还用于 Visual Studio 2012,易于访问和实现代码。
快乐编码:)
To create SDF file to DBML Using Visual Studio 2010 than, Write below line in visual studio 2010 command prompt than Enter and Execute it.
Also this SDF and DBML file in used in Visual Studio 2012 Easy to Access and Implement the code.
Happy Coding :)