Danh mục

Hướng dẫn thao tác file DataBase File bằng C#

Số trang: 6      Loại file: pdf      Dung lượng: 359.32 KB      Lượt xem: 16      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 3,000 VND Tải xuống file đầy đủ (6 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Ta lưu trữ cấu trúc của phần main header và field header vào hai lớp CMainHeader và CfieldHeader (ở đây chỉ khai báo các trường có sử dụng mà không khai báo hết các trường trong các header).
Nội dung trích xuất từ tài liệu:
Hướng dẫn thao tác file DataBase File bằng C# Hướng dẫn thao tác file DBF bằng C#1 Định nghĩa cấu trúc lưu trữ main header và field headerTa lưu trữ cấu trúc của phần main header và field header vào hai lớpCMainHeader và CfieldHeader (ở đây chỉ khai báo các trường có sử dụng màkhông khai báo hết các trường trong các header).class CMainHeader{ public byte version; public byte year, month, day; public int num_of_record; public char header_length; public char record_length; public byte[] reserved; public CMainHeader(){ }}class CFieldHeader{ public byte[] field_name; public byte type; public int record_offset; public byte field_length; public byte decimal_number; public byte[] reserved; public CFieldHeader(){ }}2 Thao tác đọc main header và field headerTa hiệu chỉnh lại constructor để thực hiện xây dựng main header và field header từchuỗi byte tương ứng.Dữ liệu đầu vào là chuỗi byte đọc được từ stream, ta xử lí chuyển byte thành dạngdữ liệu phù hợp với từng trường.class CMainHeader{ public byte version; public byte year, month, day; public int num_of_record; public char header_length; public char record_length; public byte[] reserved; // Input: chuỗi byte đọc từ stream public CMainHeader(byte[] buf) { version = buf[0]; year = buf[1]; month = buf[2]; day = buf[3]; num_of_record = BitConverter.ToInt32(buf, 4); header_length = BitConverter.ToChar(buf, 8); record_length = BitConverter.ToChar(buf, 10); reserved = new byte[20]; Array.Copy(buf, 12, reserved, 0, 20); }}class CFieldHeader{ public byte[] field_name; public byte type; public int record_offset; public byte field_length; public byte decimal_number; public byte[] reserved; public CFieldHeader(byte[] buf) { field_name = new byte[11]; Array.Copy(buf, 0, field_name, 0, 11); type = buf[11]; record_offset = BitConverter.ToInt32(buf, 12); field_length = buf[16]; decimal_number = buf[17]; reserved = new byte[14]; Array.Copy(buf, 18, reserved, 0, 14); }}3 Tổng hợp xử líKhai báo trước hai biến chứa main header và mảng field headerCMainHeader header;CFieldHeader []field_header;Xây dựng hàm xử lí đọc header file DBF//Input: tên file DBFpublic void ReadFileDBF(string filename){ // Mở file DBF System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader bReader = new System.IO.BinaryReader(filestream); // Chỉ định vị trí bắt đầu đọc dữ liệu từ stream bReader.BaseStream.Position = 0; // Đọc dữ liệu vào buffer tạm và phân tích thành các thành phần trong CMainHeader byte[] buffer = new byte[32]; bReader.Read(buffer, 0, 32); header = new CMainHeader(buffer); // Tính số field, đọc dữ liệu vào buffer tạm và phân tích thành các thành phần trong CFieldHeader int nField = (header.header_length - 1) / 32 - 1; field_header = new CFieldHeader[nField]; for (int i = 0; i < nField; i++) { bReader.Read(buffer, 0, 32); field_header[i] = new CFieldHeader(buffer); } // Đóng file DBF bReader.Close(); filestream.Close();}4 Kĩ thuật khác Chuyển từ byte sang string Ví dụ: chuyển trường field_name kiểu byte[] thành stringstring res =System.Text.Encoding.ASCII.GetString(field_header[i].field_name); HƯỚNG DẪN TẠO GIAO DIỆN ĐỒ HOẠ BẰNG MFC CHO BÀI TẬP 1Các bước sau sẽ sử dụng đối tượng ListControl của MFC để tạo bảng xuất dữ liệucho bài tập thao tác trên tập tin DBF.Bước 1: Tạo một dialog MFC dựa trên và các điều khiểnChọn loại project: MFC AppWizard (exe) -> Chọn loại ứng dụng Dialog based.Nhấn Finish. Thêm các điều khiển: một nút Open… để mở file và một ListControlChọn thuộc tính cho ListControlĐặt tên biến cho (dùng Class Wizard) ListControl: m_lstTable. Thêm phần đọc vàxử lý file dbf đã viết (yêu cầu cơ bản) vào project.Bước 2: Sử dụng hộp thoại FileDialog để mở tập tin DBF.Nhấn double click vào nút Open để tạo handler, nhấn OK để soạn thảo mã lệnh,nhập đoạn mã sau:{ CFileDialog fd(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, DBF Files(*.dbf)|*.dbf|All files|*.*|, this); if (fd.DoModal() == IDOK) { CString s= fd.GetFileName(); // Đọc nội dung file dbf bằng các hàm đã viết // nếu tham số filename kiểu char * thì thực hiện ép chuỗi: ...

Tài liệu được xem nhiều: