⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kjdbms_m.cpp

📁 一个小型数据库系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include"KJHead.h"

char user[30] ;
char password[30] ;
char sType[4], sName[10], type[30] ;
char cType ;
char iType ;

void main()
{
	char sWork[100] ;
	int i=0 , j=0 ;
	//printf("%d", '0') ;
while(1)
{
	try
	{
		CREATEiNSTANCE(spCON,Connection);		//初始化_Connection指针		
		spCON->Open("dsn=hunter","","",-1);		//调用_Connection的Open方法打开数据库
		j=0 ;
		while(j<5)
		{
			printf("\n请输入用户名: ") ;
			scanf("%s", user) ;
			getchar() ;
			i=0 ;
			printf("\n请输入密码: ") ;
			while(1)
			{
				password[i]=getch() ;
				if ((password[i]==8) && (i>=0))
				{
					if (i>0)
					{
						printf("\b \b") ;
						password[i]='\0' ;
						i-=1 ;
					}
					
				}
				else if (password[i]==13) 
					break ;
				else
				{
					printf("*") ;
					i++ ;
				}
			}
			
			
			password[i]='\0' ;
			// 初始化_Recordset的引用spRS
			CREATEiNSTANCE(spRS,Recordset);
			spRS->PutRefActiveConnection( spCON );
			
			strcpy(sWork, "") ;
			strcat(sWork, "select * from 用户信息 where 用户名 = '") ;
			strcat(sWork, user) ;
			strcat(sWork, "'") ;
			spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close ?
			if (spRS->adoEOF == -1)
			{
				printf("\n您输入的用户名不存在,请您向管理员申请!\n") ;
				spRS->Close() ;//close 41
				j++ ;
				continue ;
			}
			else if (strcmp(UC _bstr_t ( RsITEM( spRS, sTableName[7][2])), password) != 0)
			{
				printf("\n您输入的密码不正确!\n") ;
				spRS->Close() ;//close 41
				j++ ;
				continue ;
			}
			else
			{
//				spRS->Close() ;
				break ;
			}
		}

		if (j==5)//五次输入错误则退出程序
				break ;
		strcpy(sType , UC _bstr_t ( RsITEM (spRS, sTableName[7][4]))) ;
		strcpy(sName , UC _bstr_t ( RsITEM (spRS, sTableName[7][3]))) ;
		cType = sType[0] ;
		iType = cType - 48 ;		

		if (strcmp(sType, "0") == 0)
			admin() ;
		else if (strcmp(sType, "1") ==0 )
			canmou() ;
		else
			guest() ;
		
		//spCON->Close() ;
    }
    catch( _com_error & e) // 处理异常: e
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());        
		MessageBox(0,bs,bstrSource, MB_OK);
    } 

	//printf("%s", password) ;
}
	return ;
}

void canmou(void)
{
	int  iChoice ;	
	printf("\n**********\n您好 ,欢迎进入战机数据库,请注意保密!!\n**********\n") ;
	//操作数据库
	for ( ; ; )
	{		
		//打印提示信息,获取用户操作要求
		printf(" 1......战机升空\n 2......战机降落\n 3......更改密码\n 4......注销\n 5......退出\n");
		scanf("%d",&iChoice);
		
		switch (iChoice)
		{
			case 1: //读取数据
				PlaneUp() ;				
				break;//end of case 1
			
			case 2: //修改数据				
				PlaneDown() ;
				break ;
			case 3:
				ChangePsd() ;
				break ;
			case 4:
				printf("\n Thank you for using!\n") ;
				spCON->Close() ;
				return ;
			case 5:
				exit(1) ;
				break ;			
				
		}
	}	
	return ;
}
void guest(void)
{
	int  iChoice ;	
	printf("\n**********\n您好,欢迎进入战机数据库,请注意保密!!\n**********\n") ;
	//操作数据库
	for ( ; ; )
	{		
		//打印提示信息,获取用户操作要求
		printf(" 1......查询\n 2......修改密码\n 3......注销\n 4......退出\n");
		scanf("%d",&iChoice);
		
		switch (iChoice)
		{
			case 1: //读取数据
				Find() ;				
				break;//end of case 1
			
			case 2: //修改数据				
				ChangePsd() ;
				break ;
			case 3:
				printf("\n Thank you for using!\n") ;
				spCON->Close() ;
				return ;
			case 4:
				exit(1) ;
				break ;
		}
	}	
	return ;
}

void admin(void)
{
	int  iChoice ;	
	printf("\n**********\n您好,管理员,欢迎进入战机数据库,请注意保密!!\n**********\n") ;
	//操作数据库
	for ( ; ; )
	{		
		//打印提示信息,获取用户操作要求
		printf(" 1......查询\n 2......修改\n 3......添加\n 4......删除\n 5......注销\n 6......退出\n");
		scanf("%d",&iChoice);
		
		switch (iChoice)
		{
			case 1: //读取数据
				Query() ;				
				break;//end of case 1
			
			case 2: //修改数据				
				UpdateData() ;
				break ;
			case 3:
				Insert() ;
				break ;
			case 4:
				DeleteField() ;
				break ;	
			case 5:
				printf("\n Thank you for using!\n") ;
				spCON->Close() ;
				return ;
			case 6: //退出				
				exit(1);
				break;
				
		}
	}	
}

int GetTable(char * sInfor, int Count)
{
	int iChoice, i ;
	printf("\n\n请输入您要 %s 的表的号码:\n\n", sInfor) ;
	//Print the Select Information
	for (i=0; i<Count; i++)
		printf(" %d...%s\n", i+1, sTableName[i][0]) ;
	scanf("%d", &iChoice ) ;
	return (iChoice) ;
}
int GetPorperty(int * pItem, int iChoice)
{
	//pItem: 用户欲选择的属性的号码, iChoice: 用户以选择的属性所在表的号码, 内部已作减一处里
	int iItemNum ;	
	//Get which porperty want to query, and the numbers of the table
	iItemNum=1 ;
	//printf the porperty name of the table,
	while( (iItemNum<ITEMNUM) && ((strcmp(sTableName[iChoice-1][iItemNum], "")) != 0) )
	{
		printf(" %d...%s\n", iItemNum, sTableName[iChoice-1][iItemNum]) ;
		iItemNum++ ;
	}
	iItemNum-- ;
	printf("\n") ;
	//get the number of the porperty to use
	scanf("%d", pItem) ;
	return iItemNum ;
}

void Query(void)
{
	int iChoiceQuery, i ;
	int iFunc ;
	int aItem[ITEMNUM]={0} ;
	char sWork[100] , sri[100];
QueryAgain:	
	//Get which table want to query
	iChoiceQuery = GetTable(" 查询 ", TABLENUM) ;
	//Get the which function you need
	printf("\n 1...纵观全表\n 2...查询细节\n") ;
	scanf("%d", &iFunc) ;
	
	try
	{		
		switch (iFunc)
		{
			//-----------------纵观------------------
			case 1:
			// 初始化_Recordset的引用spRS
				CREATEiNSTANCE(spRS,Recordset);
				spRS->PutRefActiveConnection( spCON );
				strcpy(sWork, "") ;
				strcat(sWork, "select ") ;
				strcat(sWork, " * from ") ;
				strcat(sWork, sTableName[iChoiceQuery-1][0]) ;
				strcat(sWork, " ;") ;
				spRS->Open(sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 233
				
				PrintData(iChoiceQuery) ;
				//关闭记录集合指针
				spRS->Close(); //close 229
				
				
				break ;
			
			
			//-----------------细节------------------------------
			case 2:				
				// 初始化_Recordset的引用spRS
				CREATEiNSTANCE(spRS,Recordset);
				spRS->PutRefActiveConnection( spCON );
			
				QueryPart(iChoiceQuery) ;
				
				if (spRS->adoEOF == -1)
					printf("\n对不起,没有找到!\n") ;
				else
				{
					PrintData(iChoiceQuery) ;
				}//找到的话,打印,完毕
				//关闭记录集合指针
				spRS->Close(); //close 316
				
				
				break ;				
			case 3:
				break ;
			default:
				break ;
		}
	}
	catch( _com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
	}

	strcpy(sri, "query ");
	strcat(sri, sTableName[iChoiceQuery-1][0]) ;
	rizhi( sri ) ;

	printf("您想再次查询吗?1...是的 ") ;
	scanf("%d", &i) ;
	if (i==1)
		goto QueryAgain ;
	return ;
}

//---------------------------------------------------
void QueryPart(int iChoiceQuery )
{
			
	int iItemNum ;
	int iChoiceProperty ;
	char sEnter[100], sWork[100] ; ;
	//获取属性类型
	strcpy(sWork, "") ;
	strcat(sWork, "select * from ") ;
	strcat(sWork, sTableName[iChoiceQuery-1][0]) ;
	spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//取属性insert in close 314,  进入querypart 未关 出
	printf("\n请输入您要查询的属性号码:\n\n") ;
	//Print the Table property, to choice
	iItemNum=1 ;
	while( (iItemNum<ITEMNUM) && ((strcmp(sTableName[iChoiceQuery-1][iItemNum], "")) != 0) )
	{
		printf("\t%2d... %-17s\n", iItemNum, sTableName[iChoiceQuery-1][iItemNum]) ;
		iItemNum++ ;
	}
	printf("\n") ;
	iItemNum-- ;
	scanf("%d", &iChoiceProperty) ;
	printf("\n请输入 %s 的属性值:  ", sTableName[iChoiceQuery-1][iChoiceProperty]) ;
	scanf("%s", sEnter) ;
	//Constructe the sql sentence

	QuerySql(iChoiceQuery, iChoiceProperty, sWork, sEnter) ;
	
	//Close the spRS
	spRS->Close() ; //close 295
	
	spRS->Open(sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);	//close ? insert in  434close	delete in close non537 hav 549 query in close254 update in nonclose 342 hav 378

//-----------------------------------------------------
	return ;
}


void UpdateData(void)
{
	int iChoiceUpdate, iChoiceProperty,  iChoiceField, i ;
	char sPrimaryKey[100], sEnter[100], sWork[100], sri[100]  ;
	
	// 初始化_Recordset的引用spRS
	CREATEiNSTANCE(spRS,Recordset);
	spRS->PutRefActiveConnection( spCON );
UpdateAgain:	
	//Get the Table number you want to update
	iChoiceUpdate = GetTable(" 修改 ", TABLENUM-1) ;
	//print the property name to choose
	//iItemNum = GetPorperty(&iChoiceProperty, iChoiceUpdate) ;
	printf("\n修改之前请先查找!\n") ;
	QueryPart(iChoiceUpdate) ;
				
	if (spRS->adoEOF == -1)
	{
		printf("\n对不起,没有找到!\n") ;
		spRS->Close() ;//close 316
	}
	else
	{
		PrintData(iChoiceUpdate) ;
		//找到的话,打印,完毕
		printf("您要改哪一个?: \n") ;
		scanf("%d", &iChoiceField) ;
		spRS->MoveFirst () ;
		spRS->Move (iChoiceField-1 ) ;
		strcpy(sPrimaryKey, UC _bstr_t(RsITEM(spRS, sTableName[iChoiceUpdate-1][1]))) ;
		strcpy(sWork, "") ;
		strcat(sWork, "update ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][0]) ;
		strcat(sWork, " set ") ;
		printf("\n请输入您要修改的属性号码\n") ;
		GetPorperty( &iChoiceProperty, iChoiceUpdate ) ;
		printf("\n请输入属性值: " ) ;
		scanf("%s", sEnter) ;
		strcat(sWork, sTableName[iChoiceUpdate-1][iChoiceProperty]) ;
		strcat(sWork, " = ") ;
		//根据类型构造sql
		if (spRS->Fields ->Item [_variant_t(sTableName[iChoiceUpdate-1][iChoiceProperty])]->Type == adSmallInt)
			strcat(sWork, sEnter) ;
		else
		{
			strcat(sWork, "'") ;
			strcat(sWork, sEnter) ;
			strcat(sWork, "'") ;
		}
		strcat(sWork, " where ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][1]) ;
		strcat(sWork, " = '") ;
		strcat(sWork, sPrimaryKey) ;
		strcat(sWork, "'") ;
			
		spRS->Close() ;//close 316
		
		
		try
		{		
			spCON->Execute(sWork,&RecordsAffected,adCmdText);
			printf("Success\n") ;
			
			strcpy(sri, "update ");
			strcat(sri, sTableName[iChoiceUpdate-1][0]) ;
			rizhi( sri ) ;
		}
		catch( _com_error &e)
		{
			_bstr_t bstrSource(e.Source());
			_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
							+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
							+ _bstr_t(e.Description());
			MessageBox(0,bs,bstrSource, MB_OK);
		}
	}
	

	printf("\n您想再次修改吗?1...是的 ") ;
	scanf("%d", &i) ;
	if (i==1)
		goto UpdateAgain ;
	return ;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -