Reply
Sat 4 Sep, 2004 10:31 am
Ok, so I want to write a program that will require the use of a database... Problem is, I have no idea how to create/link/query a database to a C++ program. I don't want to use an SQL server (unless i have to)... So can anyone give me some info or point me in the right direction? Is it best to use access or foxpro or what to create the database???
thanks.
I'm not sure about c++, but I have done a lot of database access with C# and Access...perhaps looking over this will help
private static OdbcConnection objConnection;
private static string strConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=";
public static DataSet GetDataSet(string strQuery, string strTblName)
//PRE: strQuery is a SQL Query, strTblName is name of a table to create to put the data in
//POST: a DataSet is created and a table named strTblName is created
// containing the results of the SQL query
{
//OdbcConnection objConnection =GetDBConnection();
OdbcDataAdapter odbcAdapter1=GetDBAdapter(strQuery,objConnection);
DataSet Results = new DataSet("Results");
odbcAdapter1.Fill(Results,strTblName);
return Results;
}
private static OdbcDataAdapter GetDBAdapter(string strSQL,OdbcConnection objConnection)
//PRE: An SQL statement and a connection
//POST: returns an adapter which is used to execute the SQL query by GetDataSet
{
return (new OdbcDataAdapter(strSQL,objConnection));
}
private static OdbcConnection GetDBConnection()
//PRE: nothing
//POST: A new OdbcConnection object to the database indicated by the strConnection string.
{
return( new OdbcConnection(strConnection));
}
If the target OS is Windows, you should call the ODBC API (Open Database Connectivity) functions from your C or C++ program.
You should find all the information browsing the Microsoft MSDN online version (http://msdn.microsoft.com/default.aspx).
Good luck!
yep dan is right, and that's what I was doing in my c# example also, so there are probably some strong similarities