Upload .csv to SQL server using C++

不羁岁月 提交于 2019-12-13 03:31:01

问题


I am working with Visual Studio 2012 Express version under Windows 7. On the other hand, the SQL server is "SQL Server 2008 R2".

I've tried following C++ codes:

#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;

}

When I run this code, the program always stops at create table.

I wonder if I don't have the permission to create a table, thus I mannually created a table. And there is a warning pops out:

May I know if the warning shown in the above image is the reason my code fail, or I simply made mistakes in the code?

Many thanks.


回答1:


Who owns the database? You need to find your database administrator and get them to grant you permission to add tables to the database.




回答2:


You may want to check what permissions that account has been GRANTED.

You will need permissions to INSERT as well as to BULKADMIN server role.

But as you can see in this post it can get hairy depending on where the file is located. You can at



来源:https://stackoverflow.com/questions/18277243/upload-csv-to-sql-server-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!