1.14.2 Exporting into a Database

Origin C has the ability to export data from a worksheet to a specified database table. The following steps show how to export fitting summary data into a database.

  1. Set up a database named "Analysis" in MySQL, and assume it is running on the machine "Lintilla".
  2. Create a table named "FittingSummary" with 9 fields, set the data type of the first two fields as varchar(40) and the rest as double.
  3. Open OriginExe\Samples\Curve Fitting\autofit.ogw, and fill the columns on the "Data" layer with data.
  4. After recalculating, activate the "Summary" layer, and run the following code to export the result to a database.
//user should modify connect and query string according to their database settings.
//such as value of Server, Database, UID, PWD etc.
#define	STR_DB_CONN				"Driver={MySQL ODBC 3.51 Driver};	\
#define	STR_QUERY				"Select * from FittingSummary"

bool	write_wks_to_db()
	Worksheet wks = Project.ActiveLayer();
	if ( wks )
		return false;
	//connect to database "Analysis" on "Lintilla"
	string	strConn = STR_DB_CONN;
	string	strQuery = STR_QUERY;
	Object	oConn;
	oConn = CreateObject("ADODB.Connection");
	if ( !oConn )
		return error_report("Fail to create ADODB.Connection object!");
	Object	oRecordset;
	oRecordset = CreateObject("ADODB.Recordset");
	if ( !oRecordset )
		return error_report("Fail to create ADODB.Recordset object!");
	//open recordset
	oRecordset.CursorLocation = 3; //adUseClient, please refer to MSDN for details
	oRecordset.Open(strQuery, oConn, 1, 3); //adOpenKeyset, adLockOptimistic

	int iRowBegin = 0, nRows = 8; //8 rows
	int iColBegin = 0, nCols = 9; //9 columns
	//LAYWKSETRECORDSET_APPEND for appending new recordset;
	//LAYWKSETRECORDSET_REPLACE for replacing existing recordsets.
	int nOption = LAYWKSETRECORDSET_APPEND; //append.
	int nRet = wks.WriteRecordset(oRecordset, nOption,
			iRowBegin, nRows, iColBegin, nCols);
	return (0 == nRet);