用c在microsoft Visual C++2010版上编写学生信息管理系统要具体代码
时间: 2024-03-22 15:38:05 浏览: 57
抱歉,由于学生信息管理系统的具体需求和功能不同,代码也会有所不同,无法给出完整的具体代码。以下是一个简单的示例,供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
// 定义数据库连接句柄和语句句柄
SQLHDBC hdbc;
SQLHSTMT hstmt;
// 连接数据库
int ConnectDB()
{
SQLRETURN ret;
SQLCHAR *dsn = (SQLCHAR*)"学生信息管理系统";
SQLCHAR *user = (SQLCHAR*)"sa";
SQLCHAR *password = (SQLCHAR*)"123456";
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
ret = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
ret = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ret = SQLConnect(hdbc, dsn, SQL_NTS, user, SQL_NTS, password, SQL_NTS);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
printf("数据库连接失败.\n");
return -1;
}
else
{
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
return 0;
}
}
// 关闭数据库连接
void CloseDB()
{
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
// 查询学生信息
void QueryStudentInfo(char *name)
{
SQLCHAR *sql = (SQLCHAR*)"SELECT * FROM Student WHERE Name = ?";
SQLCHAR *pName = (SQLCHAR*)name;
SQLINTEGER cbName = strlen(name);
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0, pName, cbName, NULL);
SQLExecute(hstmt);
while (SQLFetch(hstmt) == SQL_SUCCESS)
{
SQLCHAR Id[20], Name[20], Gender[10], Age[10];
SQLGetData(hstmt, 1, SQL_C_CHAR, Id, 20, NULL);
SQLGetData(hstmt, 2, SQL_C_CHAR, Name, 20, NULL);
SQLGetData(hstmt, 3, SQL_C_CHAR, Gender, 10, NULL);
SQLGetData(hstmt, 4, SQL_C_CHAR, Age, 10, NULL);
printf("%s %s %s %s\n", Id, Name, Gender, Age);
}
}
// 添加学生信息
void AddStudentInfo(char *id, char *name, char *gender, char *age)
{
SQLCHAR *sql = (SQLCHAR*)"INSERT INTO Student VALUES (?, ?, ?, ?)";
SQLCHAR *pId = (SQLCHAR*)id;
SQLCHAR *pName = (SQLCHAR*)name;
SQLCHAR *pGender = (SQLCHAR*)gender;
SQLCHAR *pAge = (SQLCHAR*)age;
SQLINTEGER cbId = strlen(id);
SQLINTEGER cbName = strlen(name);
SQLINTEGER cbGender = strlen(gender);
SQLINTEGER cbAge = strlen(age);
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0, pId, cbId, NULL);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0, pName, cbName, NULL);
SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, pGender, cbGender, NULL);
SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, pAge, cbAge, NULL);
SQLExecute(hstmt);
}
int main()
{
if (ConnectDB() == 0)
{
QueryStudentInfo("张三");
AddStudentInfo("2022001", "李四", "男", "20");
CloseDB();
}
return 0;
}
```
以上示例中,使用了 SQL Server 数据库,通过 ODBC 连接方式连接数据库,并实现了查询学生信息和添加学生信息的功能。需要注意的是,以上代码仅供参考,实际开发中需要根据具体需求和功能进行修改和完善。
阅读全文