Trang chủ » Diễn Đàn » Lập trình và Phát triển Web » Microsoft .NET » Cách Search , delete và update dữ liệu trong C#?
Chủ đề đã bị khóa, bạn không thể xóa, sửa hay trả lời trong chủ đề này!
|
|
|---|
|
0
các tiền bối có thể giúp em sữa bài tập này không ! em mới học C# nên còn nhiều điều không biết vấn đề là thế này , thầy giáo em bắt làm 1 bài project về vấn đề quản lý sinh viên ,tạo 1 file text để lưu trữ dữ liệu (mã số ; tên ; số điện thoại ;xếp loại học lực ;điểm trung bình )và các dữ liệu lưu trong file txt sẽ được cách nhau bằng dấu ";" từ trong visua basic ta có thể : - nhập dữ liệu - hiển thị dữ liệu - tìm kiếm dữ liệu sinh viên từ mã số - xóa và sữa chữa dữ liệu em đã làm được phần nhập dữ liêu , hiển thị dữ liệu nhưng phần tìm kiếm ,xóa và sữa chữa em cũng đã làm nhưng làm không ra ,nó không chạy ,các vị tiền bối xin giúp em gấp , em đã mò 1 tuần rồi cũng không ra nên mới ra đây hỏi thứ 2 này em phải nộp bài rồi .xin các anh chị giúp đỡ em .thank đoạn code em đã làm : using System; using System.IO; namespace projectQ3 { class Schedule { struct details { public int maso; public string name; public int sodt; public string xeploai; public int diemtb; }; private details Testdetails; static FileStream F; StreamWriter W; StreamReader R; //nhap du lieu va luu vao tap tin public void Getdata() { Console.WriteLine("Nhap ma so sinh vien :"); Testdetails.maso = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Nhap ten sinh vien :"); Testdetails.name = Console.ReadLine(); Console.WriteLine("So dien thoai :"); Testdetails.sodt = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("xep loai cua sinh vien :"); Testdetails.xeploai = Console.ReadLine(); Console.WriteLine("diem trung binh :"); Testdetails.diemtb = Convert.ToInt32(Console.ReadLine()); F = new FileStream("C:\SinhVien.txt", FileMode.Append, FileAccess.Write); W = new StreamWriter(F); //viet du lieu vao tap tin W.Write("Maso: "); W.Write(Testdetails.maso); W.Write(";"); W.Write("Ten: "); W.Write(Testdetails.name); W.Write(";"); W.Write("Sodt: "); W.Write(Testdetails.sodt); W.Write(";"); W.Write("xeploai: "); W.Write(Testdetails.xeploai); W.WriteLine(";"); W.Write("DiemTrungbinh: "); W.Write(Testdetails.diemtb); W.Write(";"); W.Flush(); W.Close(); } //hien thi du lieu tu tap tin public void Display() { string Str; F = new FileStream("C:\SinhVien.txt", FileMode.Open, FileAccess.Read); R = new StreamReader(F); Console.Clear(); int Pos = 0; //code hien thi du lieu tren giao dien dieu khien trong 1 dinh dang while ((Str = R.ReadLine()) != null) { while (true) { Pos = Str.IndexOf(";"); if (Pos == -1) break; Console.WriteLine(Str.Substring(0, Pos)); Str = Str.Substring(Pos + 1); } Pos = 0; } R.Close(); } //tim du lieu public void Search() { string Str, Chkstr1, Chkstr2; int Ms, Pos; F = new FileStream("C:\SinhVien.txt", FileMode.Open, FileAccess.Read); R = new StreamReader(F); Console.Write("Nhap Maso(4so) "); Ms = Convert.ToInt16(Console.ReadLine()); //code de lay du lieu tu tap tin va hien thi no ra while ((Str = R.ReadLine()) != null) { Chkstr1 = "Maso: " + Ms.GetTypeCode(); Pos = Str.IndexOf(";"); Chkstr2 = Str.Substring(0, Pos); if ((Chkstr1.CompareTo(Chkstr2)) == 0) { while (true) { Pos = Str.IndexOf(";"); if (Pos == -1) break; Console.WriteLine(Str.Substring(0, Pos)); Str = Str.Substring(Pos + 1); } Pos = 0; } } R.Close(); } public void delete() { string number; string[] var; countstudent(); var = new string[count]; FileStream fs2 = new FileStream("SinhVien.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr1 = new StreamReader(fs2); Console.WriteLine("Enter ID SV you want to delete:"); number = Console.ReadLine(); if (find(number) == 0) { Console.WriteLine("ID not found"); Console.WriteLine("Press enter to continue..."); return; } for (int i = 0; i < count; i++) { var[i] = sr1.ReadLine(); } sr1.Close(); fs2.Close(); FileStream fs1 = new FileStream(@"SinhVien.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); StreamWriter sw1 = new StreamWriter(fs1); foreach (string j in var) { if (j.IndexOf(number) == -1) sw1.WriteLine(j); } Console.WriteLine("Delete completed"); Console.WriteLine("Press enter to continue..."); sw1.Flush(); sw1.Close(); fs2.Close(); } static void Main(string[] args) { Schedule S = new Schedule(); int Ch = 0; Console.Clear(); while (Ch != 5) { Console.WriteLine("[-------------------MENU-------------------]"); Console.WriteLine("| 1. Nhap DuLieu |"); Console.WriteLine("| 2. Hien Thi Danh sach Sinhvien|"); Console.WriteLine("| 3. Tim kiem theo Maso |"); Console.WriteLine("| 4. Xoa Du Lieu |"); Console.WriteLine("| 5. Exit |"); Console.WriteLine("| --> Enter Choice: <-- |"); Console.WriteLine("[------------------------------------------]"); Ch = Convert.ToInt32(Console.ReadLine()); switch (Ch) { case 1: Console.Clear(); S.Getdata(); Console.Clear(); break; case 2: Console.Clear(); S.Display(); Console.ReadLine(); Console.Clear(); break; case 3: Console.Clear(); S.Search(); Console.ReadLine(); Console.Clear(); break; //case 4: Console.Clear(); S.delete(); Console.Clear(); break; case 5: return; default: Console.WriteLine("Chi Nhap Tu 1 toi 4."); break; } } Console.ReadLine(); } } } ===>phần search và delete em làm không ra , phần delete em muốn nó đi theo kiểu chèn dữ liệu mới vào để mất đi dữ liệu cũ là được .c . |
|
|
|
0
Search , delete báo lỗi gì?
|
