本文介绍了使用Linq更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一种基于我在某处的某些字符串向数据库添加行的方法。
任何简单的linq查询代码来完成此操作?数据库已经通过以下方式连接:
public static DataClasses1DataContext database = new DataClasses1DataContext();
so我需要来自这里的代码。
谢谢
Hi, I want a way to add rows to a database based on some strings i have somewhere.
any easy linq query code to accomplish this? the database is already connected by :
public static DataClasses1DataContext database = new DataClasses1DataContext();
so I need codes from here.
Thanks
推荐答案
Coursework new_course_work = new Coursework(); //new coursework
new_course_work.Due_Date = this.dateTimePicker1.Value;//store selected date as due date
new_course_work.Title = this.textBox1.Text;//store title from user entry
new_course_work.Module_Code = moduleList.GetItemText(moduleList.SelectedItem);
//store module from selection
Globals.database.Courseworks.InsertOnSubmit(new_course_work);
Globals.database.SubmitChanges();//submit the new coursework to database
var query_ = from DataClasses1 in Globals.database.Enrols
where DataClasses1.Module_Code == new_course_work.Module_Code
select new {DataClasses1.Student_ID };//select every student ID registered on the module
foreach (var item in query_)
{
Submission new_submission = new Submission();
new_submission.Bar_Ref_ID = Globals.Bar_Ref_Gen(); //a newlyy generated bar ref ID
new_submission.CWRefID = new_course_work.CWRefID; //the CWRef ID is same as the original cw
new_submission.Status = false; //the submission status is initially set at false
new_submission.Student_ID = item.Student_ID;//the student ID is set for it
Globals.database.Submissions.InsertOnSubmit(new_submission);
Globals.database.SubmitChanges();//submit the new submission
这就是我的代码,它运行正常。任何有相同问题的人都可以使用我的尝试解决
Thats my code and it works fine. Incase any one with same problem could use mine to try and solve
这篇关于使用Linq更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!