3.6.2 Importing Database


Version Info

Minimum Origin Version Required: Origin 8 SR0

Need to do before Running Examples

Prior to running the following example, the datasource.c file need to be loaded and compiled. This can be done from script with the following command:

run.LoadOC(Originlab\datasource.c);

Import from Included Sample Access Database

We have included a small Access database in the Samples folder called stars.mdb and the following OC code shows how to control the database to import into active worksheet:

#include <..\Originlab\datasource.h>
void Import_Database_Ex()
{
	Worksheet	wks = Project.ActiveLayer();
	if( !wks )
		return;
	
	// set settings to worksheet
	string	strDatabaseFile = GetAppPath(1) + "Samples\\Import and Export\
\\stars.mdb";	

	if( strDatabaseFile.IsFile() )
	{
		string	strConn;
		strConn.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s; 
User ID=admin; Password=;", strDatabaseFile);			
		string	strSQL = "Select Stars.Index, Stars.Name, Stars.LightYears, 
Stars.Magnitude From Stars";
		
		if( db_wks_set_query(wks, strConn, strSQL) )
		{
			//do import 
			db_wks_import(wks, 0, 0, true);
			
			// remove settings from worksheet
			db_wks_set_query(wks);
			
			return;
		}
		MessageBox(NULL, "Fail to set query to worksheet.");
	}
	else
	{
		string	strErr;
		strErr.Format("Not found database file %s under %s", 
GetFileName(strDatabaseFile), GetFilePath(strDatabaseFile) );
		MessageBox(NULL, strErr);
	}
}