如果你的要求是用一種語言來開啟資料庫的話,那麼你必須首先瞭解dbf檔案的格式,這一點非常的重要。下面是一個c語言寫的讀dbf檔案的小程式,你看看吧!,你可以編輯一下,看可否達到你的要求!
#include <stdlib.h>
#include <stdio.h>
#define NFIELDS 5
#define TRUE 1
#define FALSE 0
/* DBF檔案頭結構 */
struct dbf_head{
char vers;
unsigned char yy,mm,dd;
unsigned int no_recs;
unsigned short head_len,rec_len;
char reserved[20];
};
/* DBF欄位描述結構 */
struct field_element{
char field_name[11];
char field_type;
unsigned int offset;
unsigned char field_length;
unsigned char field_decimal;
char reserved1[2];
char dbaseiv_id;
char reserved2[10];
char production_index;
};
char *dbf_fields_name[NFIELDS]={
"a", "b","c","d","e"
};
/* 全域變數 */
struct dbf_head file_head;
struct field_element *fields;
int *length;
unsigned int *offset;
/* 整形數位元組順序改變函數 */
void revert_unsigned_short(unsigned short *a)
{
unsigned short left,right;
left=right=*a;
*a=((left&0x00ff)<<8)|((right&0xff00)>>8);
}
void revert_unsigned_int(unsigned int *a)
{
unsigned int first,second,third,forth;
first=second=third=forth=*a;
*a=((first&0x000000ff)<<24)|((second&0x0000ff00)<<8)|
((third&0x00ff0000)>>8)|((forth&0xff000000)>>24);
}
/* 主函數代碼 */
void main()
{
register int i,j;
FILE *fp_dat;
char *buffer;
char *allspace;
int fields_count, matched=FALSE;
unsigned int counts;
/* 開啟dbf檔案 */
if((fp_dat=fopen("a.dbf","rb"))==NULL){
fprintf(stderr,"Cannot open dbf file to read!/n");
exit(1);
}
/* 讀取表頭紀錄 */
fseek(fp_dat,0L,SEEK_SET);
fread((void*)&file_head,sizeof(struct dbf_head),1,fp_dat);
revert_unsigned_int(&file_head.no_recs);
revert_unsigned_short(&file_head.head_len);
revert_unsigned_short(&file_head.rec_len);
/* 計算欄位數 */
fields_count=(file_head.head_len-sizeof(struct dbf_head)-1-263)/sizeof(struct field_element);
/* 開闢儲存欄位子記錄的空間 */
if((fields=(struct field_element*)malloc(sizeof(struct field_element)*fields_count))==NULL){
fprintf(stderr,"Cannot allocate memory for fields array !/n");
fclose(fp_dat);
exit(2);
}
/* 開闢儲存一條資料記錄的空間 */
if((buffer=(char*)malloc(sizeof(char)*file_head.rec_len))==NULL){
fprintf(stderr,"Cannot allocate memory for record buffer!/n");
fclose(fp_dat);
exit(3);
}
/* 開闢一個全為空白格的紀錄,以便後面做比較 */
if((allspace=(char*)malloc(sizeof(char)*file_head.rec_len))==NULL){
fprintf(stderr,"Cannot allocate memory for all_space record buffer!/n");
fclose(fp_dat);
exit(4);
}
else{
memset((void*)allspace,'/x20',file_head.rec_len-1);
allspace[file_head.rec_len]='/0';
}
/* 讀取所有的欄位子記錄,調整整形數的位元組順序 */
fread((void*)fields,sizeof(struct field_element),fields_count,fp_dat);
for(i=0;i<fields_count;i++)
revert_unsigned_int(&fields[i].offset);
/* 計算各個欄位的位元組位移量,第一位元組為刪除標記 */
fields[0].offset=1;
for(i=1;i<fields_count;i++)
fields[i].offset=fields[i-1].offset+(unsigned short)fields[i-1].field_length;
/* 開闢儲存欄位長度和位移量的數組 */
length=(int*)malloc(sizeof(int)*fields_count);
offset=(unsigned int*)malloc(sizeof(unsigned int)*fields_count);
if(length==NULL||offset==NULL){
fprintf(stderr,"Cannot allocate memory for array length or offset./n");
exit(-1);
}
/* 找到所需欄位的位移量和長度,如果沒有相應欄位,程式退出 */
for(i=0;i<NFIELDS;i++)
{
for(j=0;j<fields_count;j++)
{
if(strcmp(dbf_fields_name[i],fields[j].field_name)==0)
{
offset[i]=fields[j].offset - 1;
length[i]=fields[j].field_length;
matched=TRUE;
break;
}
if(!matched){
fprintf(stderr,"dbf file structure is invalid, field %s not found./n", dbf_fields_name[i]);
exit(-1);
}
else
matched=FALSE;
}
}
/* 定位檔案指標到資料記錄的開始位置 */
fseek(fp_dat,(long)file_head.head_len,SEEK_SET);
/* 讀取每條記錄的欄位資料 */
for(counts=0;counts<file_head.no_recs;counts++)
{
/* 如果有刪除標記,跳到下一條記錄 */
if(fgetc(fp_dat)==(int)'/x2a')
{
fseek(fp_dat,(int)file_head.rec_len-1,SEEK_CUR);
continue;
}
fread((void*)buffer,(int)file_head.rec_len-1,1,fp_dat);
buffer[file_head.rec_len]='/0';
/*去掉全為空白格的記錄行*/
if(strcmp(buffer,allspace)==0)
continue;
}
fclose(fp_dat);
/* 釋放開闢的空間 */
free(buffer);
free(allspace);
free(offset);
free(length);
}
如果有SQL2000 可以使用SQL資料庫還原方法
先隨便建立一個資料庫
把資料庫檔案作為一種備份檔案來開啟
首先登入到遠端資料庫伺服器:
1. 開啟SQL Server企業管理器,選擇要還原的資料庫,單擊滑鼠右鍵,單擊“所有任務”下的“還原資料庫”。
2. 在新彈出的視窗下的“還原”項選擇“從裝置”單選框,再單擊下面的“選擇裝置”按鈕。
3. 在新彈出的“選擇還原裝置”視窗選擇“磁碟”再單擊“添加”按鈕,選擇要還原的備份檔案。
4. 確定所有的設定,系統就會將你的資料庫進行還原。