2013-08-16 64 views
0

我在Windows 7和Visual Studio 2012版本快遞工作。另一方面.csv文件到SQL Server,SQL服務器是 「SQL Server 2008 R2的」。上傳使用C++

我已經試過以下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; 

} 

當我運行這段代碼,程序總是停在創建表。

我不知道如果我沒有創建表的權限,因此,我碼的手動創建的表。而且還有一個警告彈出:

enter image description here

我想知道,如果在上面的圖片所示的警告是我的代碼失敗的原因,還是我只是在代碼中所犯的錯誤?

非常感謝。

回答

0

誰擁有的數據庫?您需要找到數據庫管理員並讓他們授予您向數據庫添加表的權限。

0

您可能要檢查的帳戶權限已被授予什麼。

您需要的權限刀片以及對BULKADMIN server role

但你可以在this post看到它可以根據該文件位於會很麻煩。你可以在