C语言写了一个简单的通讯录

https://files.cnblogs.com/huzhongzhong/C%E8%AF%AD%E8%A8%80%E7%AE%80%E6%98%93%E9%80%9A%E8%AE%AF%E5%BD%95.zip

上面为下载地址,我传到博客园上了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include"DatebaseOperate.h"
#include"RecordM.h"
#include"Loin.h"
#include"UserManage.h"
#define N 5
extern SQLHENV m_henv;
extern SQLHDBC m_hdbc;
extern SQLHSTMT m_hstmt;

extern SQLHENV m_henv_loin;
extern SQLHDBC m_hdbc_loin;
extern SQLHSTMT m_hstmt_loin;

void mainmenu()
{	
	while(1)
	{
		int i;
		system("cls");
		//system("color d");
		printf("\n\n\n\n\n\t\t\t*******忠忠通讯录管理系统***********\n");
		printf("\t\t\t**          1.增加记录             **\n");
		printf("\t\t\t**                                 **\n");
		printf("\t\t\t**          2.删除记录             **\n");
		printf("\t\t\t**                                 **\n");
		printf("\t\t\t**          3.显示记录             **\n");
		printf("\t\t\t**                                 **\n");
		printf("\t\t\t**          4.详细查询             **\n");
		printf("\t\t\t**                                 **\n");
		printf("\t\t\t**          5.用户管理             **\n");
		printf("\t\t\t**                                 **\n");
		printf("\t\t\t**          6.关闭系统             **\n");
		printf("\t\t\t*************************************\n");
		printf("\t\t\t请选择:");
		scanf("%d",&i);
		switch(i)
		{
		case 1:AddRecord();break;
		case 2:DelteRecord();break;
		case 3:system("cls");Display();break;
		case 4:DetailRecord();break;
		case 5:UserManagement();break;
		case 6:return;break;
		default:printf("输入错误!\n");for(int j=0;j<100000000;j++){}break;
		}
		system("pause");
	}

}

void main()
{    system("color e");
for(int i=0;i<4;i++)
if(Loin()==5)
{   
	printf("登陆成功!");
	for(long i=0;i<10000000;i++)
	{
		for(long j=0;j<10;j++)
		{}
	}
	system("cls");
	break;
	
}
else
{   system("cls");system("color d");
printf("Loin error!The rest change is %d",3-i);
}
if(i==4)
{
	printf("错误次数已达到!系统自动退出!");
	return;
}

system("color d");
mainmenu();


}
SQLHENV m_henv;
SQLHDBC m_hdbc;
SQLHSTMT m_hstmt;

SQLHENV m_henv_loin;
SQLHDBC m_hdbc_loin;
SQLHSTMT m_hstmt_loin;
BOOL ConnectDBLOIN(
const char *cpDSN, const char *cpUserName, const char *cpPassword)
{
SQLRETURN retcode;
/* 分配环境句柄 */
retcode
= SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv_loin);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配环境句柄失败!");
return FALSE;
}
/* 设置ODBC环境属性 */
retcode
= SQLSetEnvAttr(m_henv_loin, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"设置ODBC环境属性失败");
return FALSE;
}
/* 分配连接句柄 */
retcode
= SQLAllocHandle(SQL_HANDLE_DBC, m_henv_loin, &m_hdbc_loin);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配连接句柄失败");
return FALSE;
}

/* 连接数据库 */
retcode
= SQLConnect(m_hdbc_loin, (SQLCHAR*) cpDSN, SQL_NTS, (SQLCHAR*) cpUserName, SQL_NTS, (SQLCHAR*) cpPassword, SQL_NTS);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"连接数据库失败");
return FALSE;
}
return TRUE;

}
BOOL ExeSqlDirectLOIN(
const char *cpSQL)
{
SQLRETURN retcode;

if(m_hstmt_loin != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt_loin);
m_hstmt_loin
= NULL;
}


if(m_hdbc_loin == NULL)
{
printf(
"没有连接数据库,请先进行联接!");
return FALSE;
}

retcode
= SQLAllocHandle(SQL_HANDLE_STMT, m_hdbc_loin, &m_hstmt_loin);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配语句句柄失败");
return FALSE;
}

retcode
= SQLExecDirect(m_hstmt_loin, (unsigned char *)cpSQL, SQL_NTS);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"执行sql语句失败");
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt_loin);
m_hstmt_loin
= NULL;
return FALSE;
}


return TRUE;
}
BOOL DisConnectLOIN()
{
if(m_hstmt_loin != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt_loin);
m_hstmt_loin
= NULL;
}

if(m_hdbc_loin != NULL)
{
SQLDisconnect(m_hdbc_loin);
SQLFreeHandle(SQL_HANDLE_DBC, m_hdbc_loin);
m_hdbc_loin
= NULL;
}

if(m_henv_loin != NULL)
{
SQLFreeHandle(SQL_HANDLE_ENV, m_henv_loin);
m_henv_loin
= NULL;
}


return TRUE;
}
BOOL ConnectDB(
const char *cpDSN, const char *cpUserName, const char *cpPassword)
{
SQLRETURN retcode;
/* 分配环境句柄 */
retcode
= SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配环境句柄失败!");
return FALSE;
}
/* 设置ODBC环境属性 */
retcode
= SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"设置ODBC环境属性失败");
return FALSE;
}
/* 分配连接句柄 */
retcode
= SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &m_hdbc);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配连接句柄失败");
return FALSE;
}

/* 连接数据库 */
retcode
= SQLConnect(m_hdbc, (SQLCHAR*) cpDSN, SQL_NTS, (SQLCHAR*) cpUserName, SQL_NTS, (SQLCHAR*) cpPassword, SQL_NTS);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"连接数据库失败");
return FALSE;
}
return TRUE;

}
BOOL DisConnect()
{
if(m_hstmt != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt);
m_hstmt
= NULL;
}

if(m_hdbc != NULL)
{
SQLDisconnect(m_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, m_hdbc);
m_hdbc
= NULL;
}

if(m_henv != NULL)
{
SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
m_henv
= NULL;
}


return TRUE;
}




BOOL ExeSqlDirect(
const char *cpSQL)
{
SQLRETURN retcode;

if(m_hstmt != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt);
m_hstmt
= NULL;
}


if(m_hdbc == NULL)
{
printf(
"没有连接数据库,请先进行联接!");
return FALSE;
}

retcode
= SQLAllocHandle(SQL_HANDLE_STMT, m_hdbc, &m_hstmt);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"分配语句句柄失败");
return FALSE;
}

retcode
= SQLExecDirect(m_hstmt, (unsigned char *)cpSQL, SQL_NTS);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO))
{
printf(
"执行sql语句失败");
SQLFreeHandle(SQL_HANDLE_STMT, m_hstmt);
m_hstmt
= NULL;
return FALSE;
}


return TRUE;
}
#include<time.h>
void enter(int x)
{
for(int i=0;i<x;i++)
printf(
"\n");
}
//tab函数
void tab(int x)
{
for(int i=0;i<x;i++)
printf(
"\t");
}
void calendar()
{
struct tm *newtime;
char tmpbuf[128];
time_t lt1;
time(
&lt1 );
newtime
=localtime(&lt1);
strftime( tmpbuf,
128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
enter(
3);
tab(
1);
printf(tmpbuf);
enter(
1);
tab(
1);
printf(
"提示:初始用户和密码为12345、6666\n");
}



int Loin()
{
SQLRETURN retcode;

retcode
= ConnectDBLOIN("ADDloin", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return -1;
}
retcode
= ExeSqlDirectLOIN("select * from Loinlist");
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return -1;
}
char Password[20];
char Name[20];
char temname[20];
char tempassword[20];
calendar();
enter(
5);
printf(
" 账户:");
gets(temname);
printf(
" 密码:");
gets(tempassword);
SQLINTEGER PasswordLen, iNameLen;
while((retcode=SQLFetch(m_hstmt_loin))!=SQL_NO_DATA)
{
SQLGetData(m_hstmt_loin,
3, SQL_C_CHAR, Password, 20, &PasswordLen);
SQLGetData(m_hstmt_loin,
2, SQL_C_CHAR, Name, 20, &iNameLen);

if((strcmp(Name,temname)==0)&&(strcmp(Password,tempassword)==0))
{
return 5;}
}

DisConnectLOIN();
return -1;
}
void Display()
{
SQLRETURN retcode;
retcode
= ConnectDB("AddressBook", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return;
}

retcode
= ExeSqlDirect("select * from LinkInfo");
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return;
}

/*获取并显示数据*/
printf(
"|------|-------------|----------------------|-----------|-------------|\n");
printf(
"| 姓名 手机号码 电子邮件地址 地址 邮编 |\n");
printf(
"|------|-------------|----------------------|-----------|-------------|\n");
while((retcode=SQLFetch(m_hstmt))!=SQL_NO_DATA)
{
char PersonID[20];
char Name[20];
char Phone[20];
char Email[20];
char address[20];
char postcode[20];
SQLINTEGER iPersonIDLen, iNameLen, iPhoneLen, iEmailLen, iAddressLen, iPostcodeLen;
SQLGetData(m_hstmt,
1, SQL_C_CHAR, PersonID, 20, &iPersonIDLen);
SQLGetData(m_hstmt,
2, SQL_C_CHAR, Name, 20, &iNameLen);
SQLGetData(m_hstmt,
3, SQL_C_CHAR, Phone, 20, &iPhoneLen);
SQLGetData(m_hstmt,
4, SQL_C_CHAR, Email, 20, &iEmailLen);
SQLGetData(m_hstmt,
5, SQL_C_CHAR, address, 20, &iAddressLen);
SQLGetData(m_hstmt,
6, SQL_C_CHAR, postcode, 20, &iPostcodeLen);
printf(
"|%s\t%s\t%s\t%s\t%s|\n",Name,Phone,Email,address,postcode);
printf(
"\n");

}

/*释放句柄*/
DisConnect();
}
void AddRecord()
{
SQLRETURN retcode;
retcode
= ConnectDB("AddressBook", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return;
}
char Name[20];
char Phone[20];
char Email[20];
char address[20];
char postcode[20];
char mysql[200];
// char a;
system("cls");
system(
"color b");
printf(
"\n\n\n\n");
printf(
"\t\t■□■□■□■□■□■□■□■□■□■□■□\n");
printf(
"\t\t■□■□■□■□★增加记录★■□■□■□■\n");
printf(
"\t\t■□■□■□■□■□■□■□■□■□■□■□\n");
printf(
"\t\t■□■Enter Name:");getchar();scanf("%s",Name);//不知道为什么要过滤掉一个字符,在另外一个地方测试却不需要
printf("\t\t■□■Enter Phone:");scanf("%s",Phone);
printf(
"\t\t■□■Enter Email:");scanf("%s",Email);
printf(
"\t\t■□■Enter address:");scanf("%s",address);
printf(
"\t\t■□■Enter postcode:");scanf("%s",postcode);
printf(
"\t\t■□■□■□■□■□■□■□■□■□■□■□\n");
sprintf(mysql,
"insert into LinkInfo(Name,Phone,Email,Address,Postcode) values('%s','%s','%s','%s','%s')",Name,Phone,Email,address,postcode);
retcode
= ExeSqlDirect(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return;
}
else
printf(
"\n\n\t\t■□■添加记录成功:■□■■□■■□■");
system(
"color F");

DisConnect();

}
void DelteRecord()
{
SQLRETURN retcode;
retcode
= ConnectDB("AddressBook", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return;
}
char Name[20];
char mysql[200];
system(
"cls");
system(
"color F");
printf(
"\n\n\n\n");
printf(
"\t\t△▲△▲△▲△▲△▲△▲△▲△▲△▲△▲△▲△\n");
printf(
"\t\t△□△□△□△□★删除记录★△□△□△□△□△\n");
printf(
"\t\t△▲△▲△▲△▲△▲△▲△▲△▲△▲△▲△▲△\n");
printf(
"\t\t■□■Enter Name:");getchar();scanf("%s",Name);puts(Name);
sprintf(mysql,
"delete from LinkInfo where Name like '%s'",Name);
retcode
= ExeSqlDirect(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return;
}
else
printf(
"\n\n\t\t■□■删除记录成功:■□■■□■■□■");
system(
"color F");

DisConnect();

}
void DetailRecord()
{
SQLRETURN retcode;
retcode
= ConnectDB("AddressBook", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return;
}
char Name[20];
char Phone[20];
char address[20];
char postcode[20];
char mysql[200];
int choice;
system(
"cls");
system(
"color F");
printf(
"\n\n\n\n");
printf(
"\t\t○ ○ ○△★按条件查询记录○★△○ ○○\n");
printf(
"\t\t○请○ ○ ○○ ○○○ ○○○ ○○○ ○○\n");
printf(
"\t\t○选○ [1]:按姓名查找 ○ ○ ○ ○\n");
printf(
"\t\t○择○ [2]:按手机查找 ○ ○ ○ ○○○\n");
printf(
"\t\t○查○ [3]:按地址查找 ○ ○ ○ ○ ○\n");
printf(
"\t\t○询○ [4]:按邮编查找 ○ ○ ○ ○○○\n");
printf(
"\t\t○方○ ○○○○○○○○○ ○○ ○○ ○\n");
printf(
"\t\t○式○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○○ \n");
printf(
"\t\t○○○○○Enter Choice:");scanf("%d",&choice);

switch(choice)
{
case 1:system("cls");printf("\n\n\n\n\t\t☆请输入查询姓名☆:");scanf("%s",Name);
sprintf(mysql,
"select * from LinkInfo where Name like '%s%s%s'","%",Name,"%");
break;
case 2:system("cls");printf("\n\n\n\n\t\t☆请输入查询手机☆:");scanf("%s",Phone);
sprintf(mysql,
"select * from LinkInfo where Phone like '%s%s%s'","%",Phone,"%");
break;
case 3:system("cls");printf("\n\n\n\n\t\t☆请输入查询地址☆:");scanf("%s",address);
sprintf(mysql,
"select * from LinkInfo where Address like '%s%s%s'","%",address,"%");
break;
case 4:system("cls");printf("\n\n\n\n\t\t☆请输入查询邮编☆:");scanf("%s",postcode);
sprintf(mysql,
"select * from LinkInfo where Postcode like '%s%s%s'","%",postcode,"%");
break;
default:printf("输入错误!\n");for(int j=0;j<100000000;j++){}break;

}
retcode
= ExeSqlDirect(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return;
}
else
printf(
"\n\n\t\t■□■查询成功:■□■■□■■□■\n");
system(
"color F");
printf(
"|------|-------------|----------------------|-----------|-------------|\n");
printf(
"| 姓名 手机号码 电子邮件地址 地址 邮编 |\n");
printf(
"|------|-------------|----------------------|-----------|-------------|\n");
while((retcode=SQLFetch(m_hstmt))!=SQL_NO_DATA)
{
char PersonID[20];
char Name[20];
char Phone[20];
char Email[20];
char address[20];
char postcode[20];
SQLINTEGER iPersonIDLen, iNameLen, iPhoneLen, iEmailLen, iAddressLen, iPostcodeLen;
SQLGetData(m_hstmt,
1, SQL_C_CHAR, PersonID, 20, &iPersonIDLen);
SQLGetData(m_hstmt,
2, SQL_C_CHAR, Name, 20, &iNameLen);
SQLGetData(m_hstmt,
3, SQL_C_CHAR, Phone, 20, &iPhoneLen);
SQLGetData(m_hstmt,
4, SQL_C_CHAR, Email, 20, &iEmailLen);
SQLGetData(m_hstmt,
5, SQL_C_CHAR, address, 20, &iAddressLen);
SQLGetData(m_hstmt,
6, SQL_C_CHAR, postcode, 20, &iPostcodeLen);
printf(
"|%s\t%s\t%s\t%s\t%s|\n",Name,Phone,Email,address,postcode);
printf(
"\n");

}
DisConnect();
}
void AllUser()
{
system(
"cls");
SQLRETURN retcode;

retcode
= ConnectDBLOIN("ADDloin", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return ;
}
retcode
= ExeSqlDirectLOIN("select * from Loinlist");
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return ;
}
char Password[20];
char Name[20];
SQLINTEGER PasswordLen, iNameLen;
printf(
"\n\n\n\n\n\t\t\t★全部用户★\n");
printf(
"\t\t◇账 户◇\t\t◇密 码◇ \n");
while((retcode=SQLFetch(m_hstmt_loin))!=SQL_NO_DATA)
{
SQLGetData(m_hstmt_loin,
3, SQL_C_CHAR, Password, 20, &PasswordLen);
SQLGetData(m_hstmt_loin,
2, SQL_C_CHAR, Name, 20, &iNameLen);

printf(
"\t\t%s\t\t\t%s\n",Name,Password);
}

DisConnectLOIN();

}
void AddCount()
{
char Password[20];
char Name[20];
char mysql[100];
system(
"cls");
SQLRETURN retcode;
printf(
"\n\n\n\n");
printf(
"\t\t\t★账户:");scanf("%s",Name);
printf(
"\t\t\t★密码:");scanf("%s",Password);
retcode
= ConnectDBLOIN("ADDloin", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return ;
}
sprintf(mysql,
"insert into Loinlist(Name,Password) values('%s','%s')",Name,Password);
retcode
= ExeSqlDirectLOIN(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return ;
}
else
{
printf(
"\t\t添加账户成功!");
}
DisConnectLOIN();
}
void DeleteCount()
{
char Name[20];
char mysql[100];
system(
"cls");
SQLRETURN retcode;
printf(
"\n\n\n\n");
printf(
"\t\t\t★要删除的账户:");scanf("%s",Name);

retcode
= ConnectDBLOIN("ADDloin", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return ;
}
sprintf(mysql,
"delete from Loinlist where Name='%s'",Name);
retcode
= ExeSqlDirectLOIN(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return ;
}
else
{
printf(
"\t\t删除用户成功!");
}
DisConnectLOIN();

}
void PasswordModifY()
{
char Password[20];
char PasswordM[20];
char Name[20];
char mysql[100];
system(
"cls");
SQLRETURN retcode;
printf(
"\n\n\n\n");
printf(
"\t\t\t★需要修改密码的账户:");scanf("%s",Name);
printf(
"\t\t\t★原始密码:");scanf("%s",Password);
printf(
"\t\t\t★修改后的密码:");scanf("%s",PasswordM);
retcode
= ConnectDBLOIN("ADDloin", "", "");
if(retcode != TRUE)
{
printf(
"连接数据库失败");
return ;
}
sprintf(mysql,
"select *from Loinlist where Name='%s'",Name);
retcode
= ExeSqlDirectLOIN(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return ;
}
SQLINTEGER PasswordLen,iNameLen;
char nn[20],pp[20];
while((retcode=SQLFetch(m_hstmt_loin))!=SQL_NO_DATA)
{
SQLGetData(m_hstmt_loin,
3, SQL_C_CHAR, nn, 20, &PasswordLen);
SQLGetData(m_hstmt_loin,
2, SQL_C_CHAR, pp, 20, &iNameLen);
}

if(strcmp(pp,Name)==0&&strcmp(nn,Password)==0)
{
sprintf(mysql,
"update Loinlist SET Password='%s'where Name='%s'",PasswordM,Name);
retcode
= ExeSqlDirectLOIN(mysql);
if(retcode != TRUE)
{
printf(
"数据库操作失败");
return ;
}
else
{
printf(
"\t\t用户%s密码修改成功!",Name);
}
}
else
{
if(strcmp(pp,Name)!=0)
{
printf(
"用户账户错误\n");

}
else if(strcmp(nn,Password)!=0)
printf(
"用户密码错误\n");
else
printf(
"此账户不存在!\n");
}
DisConnectLOIN();
}
void UserManagement()
{
int choice;
system(
"cls");
system(
"color f");
printf(
"\n\n\n\n\n\t\t\t ★用户账户管理★\n");
printf(
"\t\t\t ◇1.全部账户◇ \n");
printf(
"\t\t\t ◇2.增加用户 ◇ \n");
printf(
"\t\t\t ◇3.删除用户 ◇ \n");
printf(
"\t\t\t ◇4.修改密码◇ \n");
printf(
"\t\t\t ◇Enter Choice:");scanf("%d",&choice);
switch(choice)
{
case 1:AllUser();
break;
case 2:AddCount();
break;
case 3:DeleteCount();
break;
case 4:PasswordModifY();
break;
default:printf("输入错误!");break;
}

}

posted @ 2011-07-13 12:47  又是一年夏天  阅读(720)  评论(0编辑  收藏  举报