⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freebcp.c

📁 在Linux/Unix下面访问WINDOWS SQLSERVER 的ODBC驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
	 */	if ((*pdbproc = dbopen(login, pdata->server)) == NULL) {		fprintf(stderr, "Can't connect to server \"%s\".\n", pdata->server);		return (FALSE);	}	/* set hint if any */	if (pdata->hint) {		int erc = bcp_options(*pdbproc, BCPHINTS, (BYTE *) pdata->hint, strlen(pdata->hint));		if (erc != SUCCEED)			fprintf(stderr, "db-lib: Unable to set hint \"%s\"\n", pdata->hint);		return FALSE;	}	return (TRUE);}intfile_character(BCPPARAMDATA * pdata, DBPROCESS * dbproc, DBINT dir){	DBINT li_rowsread = 0;	int i;	int li_numcols = 0;	RETCODE ret_code = 0;	if (dir == DB_QUERYOUT) {		if (dbfcmd(dbproc, "SET FMTONLY ON %s SET FMTONLY OFF", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}	} else {		if (dbfcmd(dbproc, "SET FMTONLY ON select * from %s SET FMTONLY OFF", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}	}	if (dbsqlexec(dbproc) == FAIL) {		printf("dbsqlexec failed\n");		return FALSE;	}	while (NO_MORE_RESULTS != (ret_code = dbresults(dbproc))) {		if (ret_code == SUCCEED && li_numcols == 0) {			li_numcols = dbnumcols(dbproc);		}	}	if (0 == li_numcols) {		printf("Error in dbnumcols\n");		return FALSE;	}	if (FAIL == bcp_init(dbproc, pdata->dbobject, pdata->hostfilename, pdata->errorfile, dir))		return FALSE;	if (pdata->Eflag) {		bcp_control(dbproc, BCPKEEPIDENTITY, 1);			if (dbfcmd(dbproc, "set identity_insert %s on", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}			if (dbsqlexec(dbproc) == FAIL) {			printf("dbsqlexec failed\n");			return FALSE;		}		while (NO_MORE_RESULTS != dbresults(dbproc));	}	bcp_control(dbproc, BCPFIRST, pdata->firstrow);	bcp_control(dbproc, BCPLAST, pdata->lastrow);	bcp_control(dbproc, BCPMAXERRS, pdata->maxerrors);	if (bcp_columns(dbproc, li_numcols) == FAIL) {		printf("Error in bcp_columns.\n");		return FALSE;	}	for (i = 1; i < li_numcols; ++i) {		if (bcp_colfmt(dbproc, i, SYBCHAR, 0, -1, (const BYTE *) pdata->fieldterm,			       pdata->fieldtermlen, i) == FAIL) {			printf("Error in bcp_colfmt col %d\n", i);			return FALSE;		}	}	if (bcp_colfmt(dbproc, li_numcols, SYBCHAR, 0, -1, (const BYTE *) pdata->rowterm,		       pdata->rowtermlen, li_numcols) == FAIL) {		printf("Error in bcp_colfmt col %d\n", li_numcols);		return FALSE;	}	bcp_control(dbproc, BCPBATCH, pdata->batchsize);	printf("\nStarting copy...\n");	if (FAIL == bcp_exec(dbproc, &li_rowsread)) {		fprintf(stderr, "bcp copy %s failed\n", (dir == DB_IN) ? "in" : "out");		return FALSE;	}	printf("%d rows copied.\n", li_rowsread);	return TRUE;}intfile_native(BCPPARAMDATA * pdata, DBPROCESS * dbproc, DBINT dir){	DBINT li_rowsread = 0;	int i;	int li_numcols = 0;	int li_coltype;	int li_collen;	RETCODE ret_code = 0;	if (dir == DB_QUERYOUT) {		if (dbfcmd(dbproc, "SET FMTONLY ON %s SET FMTONLY OFF", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}	} else {		if (dbfcmd(dbproc, "SET FMTONLY ON select * from %s SET FMTONLY OFF", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}	}	if (dbsqlexec(dbproc) == FAIL) {		printf("dbsqlexec failed\n");		return FALSE;	}	while (NO_MORE_RESULTS != (ret_code = dbresults(dbproc))) {		if (ret_code == SUCCEED && li_numcols == 0) {			li_numcols = dbnumcols(dbproc);		}	}	if (0 == li_numcols) {		printf("Error in dbnumcols\n");		return FALSE;	}	if (FAIL == bcp_init(dbproc, pdata->dbobject, pdata->hostfilename, pdata->errorfile, dir))		return FALSE;	if (pdata->Eflag) {		bcp_control(dbproc, BCPKEEPIDENTITY, 1);			if (dbfcmd(dbproc, "set identity_insert %s on", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}			if (dbsqlexec(dbproc) == FAIL) {			printf("dbsqlexec failed\n");			return FALSE;		}		while (NO_MORE_RESULTS != dbresults(dbproc));	}	bcp_control(dbproc, BCPFIRST, pdata->firstrow);	bcp_control(dbproc, BCPLAST, pdata->lastrow);	bcp_control(dbproc, BCPMAXERRS, pdata->maxerrors);	if (bcp_columns(dbproc, li_numcols) == FAIL) {		printf("Error in bcp_columns.\n");		return FALSE;	}	for (i = 1; i <= li_numcols; i++) {		li_coltype = dbcoltype(dbproc, i);		li_collen = dbcollen(dbproc, i);		if (bcp_colfmt(dbproc, i, li_coltype, -1, -1, NULL, -1, i) == FAIL) {			printf("Error in bcp_colfmt col %d\n", i);			return FALSE;		}	}	printf("\nStarting copy...\n\n");	if (FAIL == bcp_exec(dbproc, &li_rowsread)) {		fprintf(stderr, "bcp copy %s failed\n", (dir == DB_IN) ? "in" : "out");		return FALSE;	}	printf("%d rows copied.\n", li_rowsread);	return TRUE;}intfile_formatted(BCPPARAMDATA * pdata, DBPROCESS * dbproc, DBINT dir){	int li_rowsread;	if (FAIL == bcp_init(dbproc, pdata->dbobject, pdata->hostfilename, pdata->errorfile, dir))		return FALSE;	if (pdata->Eflag) {		bcp_control(dbproc, BCPKEEPIDENTITY, 1);			if (dbfcmd(dbproc, "set identity_insert %s on", pdata->dbobject) == FAIL) {			printf("dbfcmd failed\n");			return FALSE;		}			if (dbsqlexec(dbproc) == FAIL) {			printf("dbsqlexec failed\n");			return FALSE;		}		while (NO_MORE_RESULTS != dbresults(dbproc));	}	bcp_control(dbproc, BCPFIRST, pdata->firstrow);	bcp_control(dbproc, BCPLAST, pdata->lastrow);	bcp_control(dbproc, BCPMAXERRS, pdata->maxerrors);	if (FAIL == bcp_readfmt(dbproc, pdata->formatfile))		return FALSE;	printf("\nStarting copy...\n\n");	if (FAIL == bcp_exec(dbproc, &li_rowsread)) {		fprintf(stderr, "bcp copy %s failed\n", (dir == DB_IN) ? "in" : "out");		return FALSE;	}	printf("%d rows copied.\n", li_rowsread);	return TRUE;}intsetoptions(DBPROCESS * dbproc, BCPPARAMDATA * params){	FILE *optFile;	char optBuf[256];	RETCODE fOK;	if (dbfcmd(dbproc, "set textsize %d ", params->textsize) == FAIL) {		fprintf(stderr, "setoptions() could not set textsize at %s:%d\n", __FILE__, __LINE__);		return FALSE;	}	/* 	 * If the option is a filename, read the SQL text from the file.  	 * Else pass the option verbatim to the server.	 */	if (params->options) {		if ((optFile = fopen(params->options, "r")) == NULL) {			if (dbfcmd(dbproc, params->options) == FAIL) {				fprintf(stderr, "setoptions() failed preparing options at %s:%d\n", __FILE__, __LINE__);				return FALSE;			}		} else {			while (fgets (optBuf, sizeof(optBuf), optFile) != NULL) {				if (dbfcmd(dbproc, optBuf) == FAIL) {					fprintf(stderr, "setoptions() failed preparing options at %s:%d\n", __FILE__, __LINE__);					return FALSE;				}			}			if (!feof (optFile)) {				perror("freebcp");        			fprintf(stderr, "error reading options file \"%s\" at %s:%d\n", params->options, __FILE__, __LINE__);				return FALSE;			}			fclose(optFile);		}		}		if (dbsqlexec(dbproc) == FAIL) {		fprintf(stderr, "setoptions() failed sending options at %s:%d\n", __FILE__, __LINE__);		return FALSE;	}		while ((fOK = dbresults(dbproc)) == SUCCEED) {		while ((fOK = dbnextrow(dbproc)) == REG_ROW);		if (fOK == FAIL) {			fprintf(stderr, "setoptions() failed sending options at %s:%d\n", __FILE__, __LINE__);			return FALSE;		}	}	if (fOK == FAIL) {		fprintf(stderr, "setoptions() failed sending options at %s:%d\n", __FILE__, __LINE__);		return FALSE;	}	return TRUE;}voidpusage(void){	fprintf(stderr, "usage:  freebcp [[database_name.]owner.]table_name {in | out} datafile\n");	fprintf(stderr, "        [-m maxerrors] [-f formatfile] [-e errfile]\n");	fprintf(stderr, "        [-F firstrow] [-L lastrow] [-b batchsize]\n");	fprintf(stderr, "        [-n] [-c] [-t field_terminator] [-r row_terminator]\n");	fprintf(stderr, "        [-U username] [-P password] [-I interfaces_file] [-S server]\n");	fprintf(stderr, "        [-v] [-d] [-h \"hint [,...]\" [-O \"set connection_option on|off, ...]\"\n");	fprintf(stderr, "        [-A packet size] [-T text or image size] [-E]\n");	fprintf(stderr, "        \n");	fprintf(stderr, "example: freebcp testdb.dbo.inserttest in inserttest.txt -S mssql -U guest -P password -c\n");}interr_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr){	static int sent = 0;	if (dberr == SYBEBBCI) { /* Batch successfully bulk copied to the server */		int batch = bcp_getbatchsize(dbproc);		printf("%d rows sent to SQL Server.\n", sent += batch);		return INT_CANCEL;	}		if (dberr) {		fprintf(stderr, "Msg %d, Level %d\n", dberr, severity);		fprintf(stderr, "%s\n\n", dberrstr);	}	else {		fprintf(stderr, "DB-LIBRARY error:\n\t");		fprintf(stderr, "%s\n", dberrstr);	}	return INT_CANCEL;}intmsg_handler(DBPROCESS * dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line){	/*	 * If it's a database change message, we'll ignore it.	 * Also ignore language change message.	 */	if (msgno == 5701 || msgno == 5703)		return (0);	printf("Msg %ld, Level %d, State %d\n", (long) msgno, severity, msgstate);	if (strlen(srvname) > 0)		printf("Server '%s', ", srvname);	if (strlen(procname) > 0)		printf("Procedure '%s', ", procname);	if (line > 0)		printf("Line %d", line);	printf("\n\t%s\n", msgtext);	return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -