从一台主机拷贝sql server数据库到另外一台主机 C++实现 给出完整代码
时间: 2024-03-02 13:49:11 浏览: 65
以下是一个简单的 C++ 程序,用于从一个主机拷贝 SQL Server 数据库到另一个主机。该程序使用 Windows API 实现文件传输和 SQL Server 数据库备份和还原。
```c++
#include <iostream>
#include <Windows.h>
#include <sql.h>
#include <sqlext.h>
using namespace std;
// 定义源和目标主机的 IP 地址和登录信息
const char* srcServer = "192.168.1.1";
const char* srcUser = "username";
const char* srcPass = "password";
const char* destServer = "192.168.1.2";
const char* destUser = "username";
const char* destPass = "password";
// 定义源和目标数据库的名称
const char* dbName = "mydatabase";
// 定义备份文件的名称
const char* backupFileName = "mydatabase.bak";
// 定义备份文件的路径
const char* backupFilePath = "C:\\backups\\";
// 定义还原文件的路径
const char* restoreFilePath = "C:\\backups\\";
// 声明 SQL Server API 函数
typedef SQLRETURN(WINAPI* SQLBACKUP)(SQLHANDLE, const SQLCHAR*, SQLSMALLINT);
// 函数声明
void BackupDatabase();
void RestoreDatabase();
int main()
{
// 备份源数据库
BackupDatabase();
// 拷贝备份文件到目标主机
char srcPath[MAX_PATH];
char destPath[MAX_PATH];
sprintf_s(srcPath, "%s%s", backupFilePath, backupFileName);
sprintf_s(destPath, "\\\\%s\\c$\\backups\\%s", destServer, backupFileName);
if (!CopyFileA(srcPath, destPath, FALSE))
{
cout << "Failed to copy backup file." << endl;
return 1;
}
// 还原目标数据库
RestoreDatabase();
cout << "Database transferred successfully." << endl;
return 0;
}
void BackupDatabase()
{
// 连接到源数据库
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
ret = SQLDriverConnectA(dbc, NULL, (SQLCHAR*)"DRIVER={SQL Server};SERVER=192.168.1.1;UID=username;PWD=password;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(ret))
{
cout << "Connected to source database." << endl;
// 备份数据库
SQLBACKUP pfnBackup;
HINSTANCE hinstLib = LoadLibraryA("sqlncli11.dll");
pfnBackup = (SQLBACKUP)GetProcAddress(hinstLib, "SqlBackup");
SQLHANDLE hBackup;
SQLCHAR szSQL[1024];
sprintf_s(szSQL, "BACKUP DATABASE [%s] TO DISK = N'%s%s' WITH INIT", dbName, backupFilePath, backupFileName);
ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
ret = SQLExecDirectA(stmt, szSQL, SQL_NTS);
if (SQL_SUCCEEDED(ret))
{
cout << "Database backed up successfully." << endl;
}
else
{
cout << "Failed to backup database." << endl;
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
FreeLibrary(hinstLib);
}
else
{
cout << "Failed to connect to source database." << endl;
}
SQLDisconnect(dbc);
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);
}
void RestoreDatabase()
{
// 连接到目标数据库
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
ret = SQLDriverConnectA(dbc, NULL, (SQLCHAR*)"DRIVER={SQL Server};SERVER=192.168.1.2;UID=username;PWD=password;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(ret))
{
cout << "Connected to destination database." << endl;
// 还原数据库
SQLHANDLE hRestore;
SQLBACKUP pfnRestore;
HINSTANCE hinstLib = LoadLibraryA("sqlncli11.dll");
pfnRestore = (SQLBACKUP)GetProcAddress(hinstLib, "SqlRestore");
SQLCHAR szSQL[1024];
sprintf_s(szSQL, "USE master RESTORE DATABASE [%s] FROM DISK = N'%s%s' WITH REPLACE, MOVE '%s' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\%s.mdf', MOVE '%s_log' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\%s_log.ldf'", dbName, restoreFilePath, backupFileName, dbName, dbName, dbName, dbName);
ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
ret = SQLExecDirectA(stmt, szSQL, SQL_NTS);
if (SQL_SUCCEEDED(ret))
{
cout << "Database restored successfully." << endl;
}
else
{
cout << "Failed to restore database." << endl;
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
FreeLibrary(hinstLib);
}
else
{
cout << "Failed to connect to destination database." << endl;
}
SQLDisconnect(dbc);
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);
}
```
请注意,此代码仅供参考,您需要根据自己的具体情况进行修改和调整。此外,为了使代码更加安全和可靠,您可能需要添加更多的错误检查和异常处理。
阅读全文