|
Ported article project to x64 platform and caught CDBException exception:
Database error: Data source name not found and no default driver specified.
Was using this driver as shown in Create New DSN dialog:
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb,*.accdb)";
Windows 10, x64, Access 2016
x86 version works fine. Any idea what is wrong?
Gennady
|
|
|
|
|
1) set the "Category" field type of Test.mdb to "Memo"
2) change the character set to unicode instead of multibyte in project properties
3) change the code accordingly (add _T operator)
4) build and try
The last character in each "Category" record gets truncated.
It has nothing to do with the output, if you put a breakpoint right after line 220 (see right below) you will see the truncation. Eg sCategory will contain "Automobil" instead of "Automobile"
recset.GetFieldValue(_T("Category"),sCategory);
Does anyone have a clue why?
This is independent of the string size.
This doesnt happen if the field type is "Text".
the whole code is here if someome is willing to have a look
void CReadDBDlg::OnRead()
{
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\Test.mdb";
int iRec = 0;
sDsn.Format(_T("ODBC;DRIVER={%s};DSN='';DBQ=%s"),sDriver,sFile);
TRY
{
database.Open(NULL,false,false,sDsn);
CRecordset recset( &database );
SqlString = "SELECT CatID, Category "
"FROM Categories";
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
ResetListControl();
ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
m_ListControl.InsertColumn(0,_T("Category Id"),LVCFMT_LEFT,-1,0);
m_ListControl.InsertColumn(1,_T("Category"),LVCFMT_LEFT,-1,1);
m_ListControl.SetColumnWidth(0, 120);
m_ListControl.SetColumnWidth(1, 200);
while( !recset.IsEOF() )
{
recset.GetFieldValue(_T("CatID"),sCatID);
recset.GetFieldValue(_T("Category"),sCategory);
iRec = m_ListControl.InsertItem(0,sCatID,0);
m_ListControl.SetItemText(0,1,sCategory);
recset.MoveNext();
}
database.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
|
|
|
|
|
hi dear zhaque
thanks for your article
can you help me?
i want to connect to a "mdf" database,but i don't know what string should i use.
what do i do?
thanks.
|
|
|
|
|
Hi,
Your tutorial is excellent... Have you manage to modify you code with the ability to write data in the Test.mdb file. If not Can you show a tutorial too?
Hoping for positive response.
Roel123
|
|
|
|
|
thanks
i need it
and how can i connect to a sql or database (.mdf) file and query on that?
i'm working visual c 2010.
|
|
|
|
|
I included the Try/Catch blocks in the code. The program builds successfully. When i run, it runs successfully, displays the list control as expected. When i click the Read button. I get an exception. Am given the message Unhandled exception at 0X00414ea6 in ReadDB.exe : 0XC0000005 : Access violation reading location 0X00000001. At the bottom of the message box there is Break, Continue, Ignore buttons.
I click the Break button and a file atlsimpstr.h is opened. the yellow arrow for break points on the return statement in code fragment below.
CStringData* GetData() const throw()
{
return (reinterpret_cast<cstringdata*>(m_pszData)-1);
}
Please help me with this.
|
|
|
|
|
Thanks for the response. I have put the Try/Catch blocks on opening the database and on opening recordset CRecordset recset. Still i get the same problem. The program runs and displays the dialog box containing the List Control. When i click the Read button so that it reads data from the Database on to the List Control, instead it populates the list control with gridelines and doesnot show any data from the database. Its presents an empty List Control with no data from the database and no error messages. I will be glad for your help.
modified on Friday, March 19, 2010 5:26 PM
|
|
|
|
|
Seriously...why would you put that much code into a try/catch block? You need to target the statements that could throw an exception. A lot of the code within that Try/Catch will never throw an exception and if you do get an exception as a result of something within that code, it will be harder to figure out what your problem was.
|
|
|
|
|
Hi people, am new in VC++ programming and using Microsoft Visual C++.NET. I have run the program in the above article but it generated errors on building. I modified the function below as follows. ResetListControl()function remained as it was. It runs without errors but doesnot show the data in database. Please help.. I will be grateful
void CReadDBDlg::OnRead()
{
// TODO: Add your control notification handler code here
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "C:\\Documents and Settings\\Owner\\Desktop\\mfcdataAccess\\readDb\\ReadDB_demo\\Test.mdb";
int iRec = 0;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories";
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Reset List control if there is any data
ResetListControl();
// populate Grids
ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
// Column width and heading
m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
m_ListControl.InsertColumn(1,"Category",LVCFMT_LEFT,-1,1);
m_ListControl.SetColumnWidth(0, 120);
m_ListControl.SetColumnWidth(1, 200);
short nFields = recset.GetODBCFieldCount( ); //get total no. of fields
CDBVariant varValue;
// Loop through each record
while( !recset.IsEOF() )
{
for( short index = 0; index < nFields; index++ )
{
// Copy each column into a variable
recset.GetFieldValue( index, varValue );
switch ( index ) {
case 0: // field no 1
if ( varValue.m_dwType == 8 )
//Insert values into the list control
iRec = m_ListControl.InsertItem(0,varValue.m_pstring->GetBuffer(),0);
break;
case 1: // field no 2
if ( varValue.m_dwType == 8 )
m_ListControl.SetItemText(0,1,varValue.m_pstring->GetBuffer());
break;
}
}
// goto next record
recset.MoveNext();
}
// Close the database
recset.Close();
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
// Reset List control
void CReadDBDlg::ResetListControl()
{
m_ListControl.DeleteAllItems();
int iNbrOfColumns;
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
if (pHeader)
{
iNbrOfColumns = pHeader->GetItemCount();
}
for (int i = iNbrOfColumns; i >= 0; i--)
{
m_ListControl.DeleteColumn(i);
}
}
|
|
|
|
|
Hi all,
I got an error when querying like this
CString SqlString;
strSqlString.Format(_T("select * from IOData where HolderName = %s and IODate = %s"), strHolderName, mstrStartDate);
database.ExecuteSQL(SqlString);
Error shown is
"Syntax Error too mny arguments"
Please give me an idea about it
modified on Monday, June 30, 2008 2:35 AM
|
|
|
|
|
You need single quote on both side of %s e.g. '%s'
corrected:
strSqlString.Format(_T("select * from IOData where HolderName = '%s' and IODate = '%s'"), strHolderName, mstrStartDate);
Try this, this will work.
|
|
|
|
|
Hi all,
I got an error when querying like this
CString strStartDate;
strStartDate.Format(_T("#%s#"), strDate);
CString SqlString;
SqlString.Format("select * from IOData where IODate = strStartDate");
database.ExecuteSQL(SqlString);
Error shown is
"Syntax Error in date in query expression
IODate = strStartDate"
Please give me an idea about it
|
|
|
|
|
This is really helpful for a beginner. Could you please explain some basics on DSN?
Jes
|
|
|
|
|
Can any one please explain me how???
|
|
|
|
|
Hello,
To the test.mdb file, I added a new table. The name of the table is Makes. This table contains 3 fields: ID, CatID, and Make. In the code, I changed the value of SqlString to "SELECT Categories.Category, Makes.Make FROM Categories, Makes WHERE Categories.CatID=Makes.CatID". When I run the program, I get a message box with the following message: Database error: Type mismatch in expression. Any help is appreciated.
Mike
|
|
|
|
|
Please help me to create MS Access file with the help of mfc code(ODBC)
|
|
|
|
|
Hi,
Even I've used the same method.
How do I get the connection mentioned in main dlg box,apply to even modal dlgboxes.
My problem is Ican dynamically retrieve records in main dlgbox but failed to do so in modal dlgboxes.I've included main dlgbox header file,even then it doesn't work.
WHT could be the problem?
swapna_signsin
|
|
|
|
|
i am an engg.student of comp.science i need to make connectivity of c/c++ with access/sql database . i need the code that can run on a compiler which is common to use like dev-c++ etc
if u an help me then plz do
|
|
|
|
|
Hi, I was looked for what is offered by CDatabase in MS Access
even after adding the process.h still I get this Error
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/database.exe : fatal error LNK1120: 2 unresolved externals
Any Help would be appreciated. I am using this under Win32 API programming:
#include <afxdb.h>
#include <odbcinst.h>
#include <process.h>
{
....some other codes
}
void FillListBox (HWND hwndList)
{
CDatabase database;
CString SqlString;
CString sCatID, sCategory;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\test\\Test.mdb";
int iRec = 0;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories";
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Loop through each record
while( !recset.IsEOF() )
{
// Copy each column into a variable
recset.GetFieldValue("CatID",sCatID);
recset.GetFieldValue("Category",sCategory);
/*
// I thought this SendMessage will work!
CString szFormatId, szFormatCat;
szFormatId.Format("%d",sCatID);
szFormatCat.Format("%s",sCategory);
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szFormatId) ;
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szFormatCat) ;
// But it give following error
// : error C2440: 'type cast' : cannot convert from 'class CString' to 'long'
// No user-defined-conversion operator available that can perform this conversion,
// or the operator cannot be called
*/
// goto next record
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
|
|
|
|
|
You need to include MFC. In the project options (I believe) there is an option on one of the tabs that says Include MFC in a Static Library. That button needs to be checked and then the project should compile.
Josh
|
|
|
|
|
Please add these header files and try, hopefully it will work.
#include "afxdb.h"
#include "odbcinst.h"
-- modified at 13:25 Monday 31st October, 2005
|
|
|
|
|
Hello,
It appears as soon as it gets to recSet.Open(...) I get this error :
"No Columns were bound prior to calling SQL FetchScroll/SqlExtendedFetch"
any idea what causing this ?
thanks
|
|
|
|
|
Sure, I also notice that CRecordset can not open with "Dynaset". This problem seems having no solution. This means you can not update the tables in Access through CRecordset. ODBC can not be used anymore?
|
|
|
|
|
i have done a mfc app, that lists all the datas of a particular database(MS Access) in a List control box... now i want to get the particular record when clicking on an items listed in List Control Box.... how can i do this ??
anybody please help.. if possible explain in details sir...
thanks in advance....
sakesh
|
|
|
|
|
GetFieldValue supports Unicode?
I have some unicode data in my database. After this function call ( GetFieldValue("Descript",sDescript) I get always "??????" characters. For debugging I have used
fout << (const char*) sDescript << endl;
tnx
|
|
|
|
|