static void Main1(string[] args) { StudentDataContext SDC = new StudentDataContext(); foreach (Student st in SDC.Students.ToList()) { Console.WriteLine(st.Id + " : " + st.Name); } Console.WriteLine(" ========= "); Student s = new Student(); s.Id = 4; s.Name = "PLS"; s.Age = 20; SDC.Students.InsertOnSubmit(s); SDC.SubmitChanges(); foreach (Student st in SDC.Students.ToList()) { Console.WriteLine(st.Id + " : " + st.Name); } Console.WriteLine(" ========= "); SDC.Students.DeleteOnSubmit( SDC.Students.SingleOrDefault(std => std.Id == 2) ); SDC.SubmitChanges(); foreach (Student st in SDC.Students.ToList()) { Console.WriteLine(st.Id + " : " + st.Name); } Console.WriteLine(" ========= "); Console.ReadKey(); }
public IHttpActionResult editStudent([FromBody] SinhVien sinhVien) { try { SinhVien sv = st.SinhViens.FirstOrDefault(x => x.maSV == sinhVien.maSV); if (sv == null) { return(StatusCode(HttpStatusCode.NoContent)); } if (sinhVien.hoTenSV != null) { sv.hoTenSV = sinhVien.hoTenSV; } if (sinhVien.diaChi != null) { sv.diaChi = sinhVien.diaChi; } if (sinhVien.email != null) { sv.email = sinhVien.email; } if (sinhVien.soDienThoai != null) { sv.soDienThoai = sinhVien.soDienThoai; } st.SubmitChanges(); return(Ok(sv)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult addStudent([FromBody] SinhVien sinhVien) { StudentDataContext st = new StudentDataContext(); try { //SinhVien sv = st.SinhViens.FirstOrDefault(x => x.maSV == sinhVien.maSV); //if (sv.maSV == sinhVien.maSV) //{ // return Ok("Đã trùng Mã Sinh Viên"); //} //if (string.IsNullOrEmpty(sinhVien.maSV)) //{ // return Ok(new HttpResponseMessage(HttpStatusCode.NotModified)); //} //insert data st.SinhViens.InsertOnSubmit(sinhVien); st.SubmitChanges(); return(Ok(new HttpResponseMessage(HttpStatusCode.NotModified))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
//執行更新Id=1的資料 private void button4_Click(object sender, EventArgs e) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\temp\tempdatacontext.log")) { StudentDataContext db = new StudentDataContext(); db.Log = sw; //assigned streamwriter to the log property of datacontext student myStudent = db.student.Single(p => p.Id == 1); myStudent.Name = "TonyCHEN"; db.SubmitChanges(); } }
//新增一筆資料 private void button1_Click(object sender, EventArgs e) { StudentDataContext db = new StudentDataContext(); //可直接使用db的table(因為已經被自動宣告了class)來宣告一筆資料 student newStudent = new student() { Id = 1, Name = "Tony", Gender = "Male" }; db.student.InsertOnSubmit(newStudent); db.SubmitChanges(); MessageBox.Show("Insert success!!"); }
protected void Insert_Record_Click(object sender, EventArgs e) { StudentDataContext db = new StudentDataContext(); Student s = new Student(); s.name = name.Text; s.sem = Int32.Parse(sem.Text); s.cpi = float.Parse(cpi.Text); s.contactno = long.Parse(cno.Text); s.email = email.Text; db.Students.InsertOnSubmit(s); db.SubmitChanges(); Label1.Text = "Record Inserted Successfully"; Label1.ForeColor = System.Drawing.Color.Green; }
//新增一筆資料並log SQL的命令 private void button3_Click_1(object sender, EventArgs e) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\temp\tempdatacontext.log")) { StudentDataContext db = new StudentDataContext(); db.Log = sw; //可直接使用db的table(因為已經被自動宣告了class)來宣告一筆資料 student newStudent = new student() { Id = 3, Name = "Sam", Gender = "Male" }; db.student.InsertOnSubmit(newStudent); db.SubmitChanges(); } }