我在 Windows 7 下使用 Visual Studio 2012 Express 版本。另一方面,SQL 服务器是“SQL Server 2008 R2”。
我尝试过以下 C++ 代码:
#include <iostream>
#include <windows.h>
#include <string>
#include <sql.h>
#include <sqltypes.h>
#include <sqlext.h>
using namespace std;
void main()
{
clock_t start = clock();
SQLHANDLE sqlevent, sqlconnection, sqlstatement;
if(SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlevent))
{
cout<<"The sqlevent has failed to be created."<<endl;
system("pause");
return;
}
if(SQL_SUCCESS != SQLSetEnvAttr(sqlevent, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
{
cout<<"The sqlevent has failed to be initialized."<<endl;
system("pause");
return;
}
if(SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlevent, &sqlconnection))
{
cout<<"The sqlconnection has failed to be created."<<endl;
system("pause");
return;
}
SQLCHAR retstring[10000];
SQLDriverConnect(sqlconnection, NULL, (SQLCHAR*)("DRIVER={SQL Server Native Client 10.0};SERVER=DATASERVER;DATABASE=DATABASE;UID=CrystalReports;PWD=PASSWORD"), SQL_NTS, retstring, 10000, NULL, SQL_DRIVER_NOPROMPT);
if(SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlconnection, &sqlstatement))
{
cout<<"The sqlstatement has failed to be created."<<endl;
system("pause");
return;
}
string commandline;
commandline = "CREATE TABLE NEW_TABLE ( ID VARCHAR(10), AGE FLOAT) GO ";
if(SQL_SUCCESS != SQLExecDirect(sqlstatement, (SQLCHAR*)(commandline.c_str()), SQL_NTS))
{
cout<<"The create table sql command has failed to excute."<<endl;
system("pause");
return;
}
commandline = "BULK INSERT NEW_TABLE FROM 'C:/temp/datafile.csv' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n') GO";
if(SQL_SUCCESS != SQLExecDirect(sqlstatement, (SQLCHAR*)(commandline.c_str()), SQL_NTS))
{
cout<<"The import sql command has failed to excute."<<endl;
system("pause");
return;
}
clock_t end = clock();
cout<<"Import from .csv file to SQL Server costs "<<((end-start)/double(CLOCKS_PER_SEC))<<" seconds."<<endl;
system("pause");
return;
}
当我运行这段代码时,程序总是在创建表处停止。
我想知道我是否没有创建表的权限,因此我手动创建了一个表。并且弹出一个警告:
我可以知道上图中显示的警告是我的代码失败的原因,还是我只是在代码中犯了错误?
非常感谢。