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

📄 wide_rpc.c

📁 sybase数据库ct library的开发,使用了所以有函数
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
** RPC COMMAND EXAMPLE PROGRAM: For XNL feature
** ---------------------------------------------
**
** Description
** -----------
**	This example program demonstrates sending a RPC command 
**	to a Server and the processing of row, parameter, and status
**	results returned from the remote procedure.
**
**	The example uses standard ANSI C for input/output and
**	memory management.
**
**	All work is performed synchronously.
**
** Routines Used
** -------------
**	All the required routines required for establishing and closing
**	a connection to a server, sending a language command to a
**	server, and processing row results.
**
**	In addition, the following routines were used to demonstrate
**	sending and processing a RPC command:
** 
**	ct_param()
**	ct_bind()
**	cs_convert()
**	ct_res_info()
**	ct_command()
** 
** Input
** -----
**	No input is required. Information normally required
**	from the user is retrieved from global variables defined
**	in the example header files.
**
** Output
** ------
**	The example program displays the row results
**	status results, parameter results, and server message
**	returned by the remote procedure.
**
** Server Dependencies
** -------------------
**	If connecting to an Open Server, the Open Server must be able 
**	to handle language commands intended for a SQL Server.
**
** Server Version
** --------------
**	5.0, 4.9.5, 4.9.1
**
**
** Server Tables
** -------------
**	No tables are used. All data is internal to the 
**
** Algorithm
** ----------
**	Initialize Client-Library.
**
**	install message handling callbacks.
**
**	Establish a connections. 
**
**	Create a database.
**
**	Create a stored procedure.
**
**	Execute the stored procedure.
**
**	Retrieve and display the results returned from the stored 
**	procedure.
**
**	Perform cleanup by dropping the database and the connection,
**	deallocating memory allocated for commands, connections, and 
**	contexts, and exiting Client-Library.
**
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctpublic.h>
#include "example.h"
#include "exutils.h"

#if !lint
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* !lint */

/*****************************************************************************
** 
** defines and globals used.
** 
*****************************************************************************/
/*
** Global names used in this module
*/
CS_CHAR *Ex_appname = "rpc_example";
CS_CHAR	*Ex_dbname = "sampledb";
CS_CHAR *Ex_server   = EX_SERVER;
CS_CHAR *Ex_username = EX_USERNAME;
CS_CHAR *Ex_password = EX_PASSWORD;

/*
** Prototypes for routines in the example code.
*/
CS_STATIC CS_RETCODE CreateStoredProcedure PROTOTYPE((
	CS_CONNECTION *connection
	));
CS_STATIC CS_RETCODE InstallNulls PROTOTYPE((
	CS_CONTEXT *context
	));
CS_STATIC CS_RETCODE BuildRpcCommand PROTOTYPE((
	CS_COMMAND *cmd
	));
CS_STATIC CS_RETCODE DoRpc PROTOTYPE((
	CS_CONNECTION *connection
	));


/*
** main()
** 
** Purpose:
**	Entry point for example program.
** 
** Parameters:
**	None, argc and argv will not be used.
**
** Return:
** 	EX_EXIT_ERROR  or EX_EXIT_SUCCEED
**
*/

int
main()
{
	CS_CONTEXT	*context;
	CS_CONNECTION	*connection;
	CS_RETCODE	retcode;
	
	EX_SCREEN_INIT();

	fprintf(stdout, "RPC Example\n");
	fflush(stdout);

	/* 
	** Allocate a context structure and initialize Client-Library 
	*/
	retcode = ex_init(&context);
	if (retcode != CS_SUCCEED)
	{
		ex_panic("ex_init failed");
	}

	/* 
	** Allocate a connection structure, set its properties, and  
	** establish a connection.
	*/
	retcode = ex_connect(context, &connection, Ex_appname,
				Ex_username, Ex_password, Ex_server);

	/*
	** Create a database for the sample program and change to it.
	*/
	if (retcode == CS_SUCCEED)
	{
		retcode = ex_create_db(connection, Ex_dbname);
	}

	/*
	** Create a stored procedure to execute
	*/
	if (retcode == CS_SUCCEED)
	{
		retcode = CreateStoredProcedure(connection);
	}

	/*
	** Install our null values to display
	*/
	if (retcode == CS_SUCCEED)
	{
		retcode = InstallNulls(context);
	}

	/*
	** Execute the the newly created RPC
	*/
	if (retcode == CS_SUCCEED)
	{
		retcode = DoRpc(connection);

		/*
		** Remove the database that was created.  The error
		** code (if not succeed), will be used in the cleanup
		** routines.
		*/
		if (retcode == CS_SUCCEED)
		{
			retcode = ex_remove_db(connection, Ex_dbname);
		}
		else
		{
			(void)ex_remove_db(connection, Ex_dbname);
		}
	}

	/*
	** Deallocate the allocated structures, close the connection,
	** and exit Client-Library.
	*/
	if (connection != NULL)
	{
		retcode = ex_con_cleanup(connection, retcode);
	}
	
	if (context != NULL)
	{
		retcode = ex_ctx_cleanup(context, retcode);
	}

	return (retcode == CS_SUCCEED) ? EX_EXIT_SUCCEED : EX_EXIT_FAIL;
}

/*
** CreateStoredProcedure()
**
** Type of function:
** 	rpc program internal api
**
** Purpose:
** 	Create a stored procedure in the server for subsequent.
**
** Parameters:
** 	connection	- Pointer to CS_CONNECTION structure.
**
** Return:
**	CS_SUCCEED if rpc was created.
**	Otherwise a Client-Library failure code.
*/

CS_STATIC CS_RETCODE 
CreateStoredProcedure(connection)
CS_CONNECTION   *connection;
{

	CS_RETCODE	retcode;
	CS_CHAR		*cmdbuf;
	
        if ((retcode = ex_use_db(connection, Ex_dbname)) != CS_SUCCEED)
        {
                ex_error("CreateStoredProcedure: ex_use_db() failed");
                return retcode;
        }

	/* 
	** Allocate the buffer for the command string.
	*/
	cmdbuf = (CS_CHAR *) malloc(EX_BUFSIZE);
	if (cmdbuf == (CS_CHAR *)NULL)
	{
		ex_error("CreateTable: malloc() failed");
		return CS_MEM_ERROR;
	}

	/* 
	** Build the command for creating the stored procedure.
	** First, drop the stored procedure if it already exits.
	*/
	strcpy(cmdbuf, "if exists (select name from sysobjects \
			where type = \"P\" and name = \"sample_rpc\") \
			Begin	\
				drop proc sample_rpc	\
			End ");
        if ((retcode = ex_execute_cmd(connection, cmdbuf)) != CS_SUCCEED)
        {
                ex_error("CreateTable: ex_execute_cmd(drop table) failed");
		free (cmdbuf);
		return retcode;
	}
		
	/* 
	** Define the parameters.
	*/
	strcpy(cmdbuf, "create proc sample_rpc (@intparam int, \
		@sintparam smallint output, @floatparam float output, \
		@moneyparam money output,  \
		@dateparam datetime output, @charparam char(300) output, \
		@binaryparam	binary(20) output) \
		as ");
	/* 
	** Define queries to return row results, assign parameter values,
	** and return a message result.
	*/
	strcat(cmdbuf, "select @intparam, @sintparam, @floatparam, @moneyparam, \
		@dateparam, @charparam, @binaryparam \
		select @sintparam = @sintparam + @intparam \
		select @floatparam = @floatparam + @intparam \
		select @moneyparam = @moneyparam + convert(money, @intparam) \
		select @dateparam = getdate() \
		select @charparam = \"The char parameters\" + replicate('a',260) \
		select @binaryparam = @binaryparam \
		print \"This is the message printed out by sample_rpc.\"");
        if ((retcode = ex_execute_cmd(connection, cmdbuf)) != CS_SUCCEED)
        {
                ex_error("CreateTable: ex_execute_cmd(drop table) failed");
	}
		
	free (cmdbuf);
	return retcode;
}

/*
** InstallNulls()
**
** Type of function:
** 	rpc program internal api
**
** Purpose:
** 	Install the character string "NULL" as the default null value
**	when bind to character columns.
**
** Parameters:
** 	context	- Pointer to CS_CONTEXT structure.
**
** Return:
**	CS_SUCCEED if null was installed.
**	Otherwise a Client-Library failure code.
*/

CS_STATIC CS_RETCODE 
InstallNulls(context)
CS_CONTEXT   *context;
{
	CS_DATAFMT	datafmt;

	memset(&datafmt, 0, sizeof (datafmt));
	datafmt.datatype = CS_CHAR_TYPE;
	datafmt.status = CS_FMT_UNUSED;
	datafmt.locale = NULL;
	
	return cs_setnull(context, &datafmt, "NULL", strlen("NULL"));
}

/*
** BuildRpcCommand()
**
** Type of function:
** 	rpc program internal api
**
** Purpose:
**	This routine contructs the parameters list for the rpc to execute
**
** Parameters:
** 	cmd	- Pointer to CS_COMMAND structure.
**
** Return:
**	CS_SUCCEED if rpc command was contructed
**	Otherwise a Client-Library failure code.
** 
*/

CS_STATIC CS_RETCODE 
BuildRpcCommand(cmd)
CS_COMMAND   *cmd;

⌨️ 快捷键说明

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