| | |
Visual C++, Database connection, please help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello.
I'm using Visual C++6.0, and Windows XP. I am trying to connect to a database. I want to add some records to it, reading the values out of edit boxes.
Here is my Code:
But I get this error:
Microsoft Visual C++ Library
Runtime Error
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Your help will be greatly appreciated
Thanks,
Emily
I'm using Visual C++6.0, and Windows XP. I am trying to connect to a database. I want to add some records to it, reading the values out of edit boxes.
Here is my Code:
cpp Syntax (Toggle Plain Text)
CDialog::EnableConnections(); ::CoInitialize(NULL); _ConnectionPtr con; con.CreateInstance(__uuidof(Connection)); con->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\Documents and Settings\\204504519\\Desktop\\webcam.mdb",_bstr_t (""),_bstr_t(""),adOpenUnspecified); _CommandPtr cmd; cmd.CreateInstance(__uuidof(Command)); cmd->ActiveConnection = con; cmd->CommandText = "Select * from Main"; _RecordsetPtr pRs; pRs.CreateInstance(__uuidof(Recordset)); pRs ->putref_Source(cmd); _variant_t vNull; vNull.vt = VT_ERROR; vNull.scode = DISP_E_PARAMNOTFOUND; pRs->Open(vNull,vNull,adOpenDynamic,adLockOptimistic,adCmdUnknown); COleSafeArray vaFieldlist; vaFieldlist.CreateOneDim(VT_VARIANT,6); long lArrayIndex[1]; lArrayIndex[0] = 0; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("UserName"))); lArrayIndex[0] = 1; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Name"))); lArrayIndex[0] = 2; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Surname"))); lArrayIndex[0] = 3; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("IDNum"))); lArrayIndex[0] = 4; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Password"))); lArrayIndex[0] = 5; vaFieldlist.PutElement(lArrayIndex,&(_variant_t("UserType"))); COleSafeArray vaValuelist; vaValuelist.CreateOneDim(VT_VARIANT,6); lArrayIndex[0] = 0; vaValuelist.PutElement(lArrayIndex,&(_variant_t(""))); lArrayIndex[0] = 1; vaValuelist.PutElement(lArrayIndex,&(_variant_t(""))); lArrayIndex[0] = 2; vaValuelist.PutElement(lArrayIndex,&(_variant_t(""))); lArrayIndex[0] = 3; vaValuelist.PutElement(lArrayIndex,&(_variant_t(""))); lArrayIndex[0] = 4; vaValuelist.PutElement(lArrayIndex,&(_variant_t(""))); lArrayIndex[0] = 5; vaValuelist.PutElement(lArrayIndex,&(_variant_t("Client"))); pRs->AddNew(vaFieldlist,vaValuelist); UpdateData(TRUE); _variant_t vName,vVal; vName.SetString("UserName"); vVal.SetString(m_UName); pRs->Update(vName,vVal); vName.SetString("Name"); vVal.SetString(m_Name); pRs->Update(vName,vVal); vName.SetString("Surname"); vVal.SetString(m_Surname); pRs->Update(vName,vVal); vName.SetString("IDNum"); vVal.SetString(m_IDNum); pRs->Update(vName,vVal); vName.SetString("Password"); vVal.SetString(m_Password); pRs->Update(vName,vVal); vName.SetString("UserName"); vVal.SetString(m_UName); pRs->Update(vName,vVal); vName.SetString("UserType"); vVal.SetString(m_UType); pRs->Update(vName,vVal); pRs->Close(); con->Close(); CoUninitialize(); CDialog::OnOK(); } void CNew::OnCancel() { CDialog::OnOK();
But I get this error:
Microsoft Visual C++ Library
Runtime Error
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Your help will be greatly appreciated
Thanks,
Emily
Last edited by WaltP; Dec 6th, 2006 at 4:54 am. Reason: Added code tags -- please read the announcement for BBCODES at the top of the forum
without thuroughly going through your code the best i can give you is this:
try not to specificy the location of tyour database from the connection string.
(which is what it appears you are doing).
if you are using an SQL server like SQL Server or MySQL it is quite simple to establish a connection.
simply give the driver, the username and the password.
open the connection and then using SQL select the database you wish to use.
have you also tried using break points to see at which point in the code it is actually failing at?
you could just be missing something very simple.
EDIT:
also this seems to be an ACcess database, is this using OleDB or ODBC?
there is a difference in the string used to connect.
ODBC:
Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;
OleDB
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
try not to specificy the location of tyour database from the connection string.
(which is what it appears you are doing).
if you are using an SQL server like SQL Server or MySQL it is quite simple to establish a connection.
simply give the driver, the username and the password.
open the connection and then using SQL select the database you wish to use.
have you also tried using break points to see at which point in the code it is actually failing at?
you could just be missing something very simple.
EDIT:
also this seems to be an ACcess database, is this using OleDB or ODBC?
there is a difference in the string used to connect.
ODBC:
Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;
OleDB
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
Last edited by Killer_Typo; Dec 7th, 2006 at 1:16 am.
Dont forget to spread the reputation to those that deserve!
Hello :cheesy:
I've got the code working. I replaced the code:
with this:
...and it works perfectly...
Thanks for your help,
Emily
I've got the code working. I replaced the code:
C++ Syntax (Toggle Plain Text)
con->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\204504519\\Desktop\\webcam.mdb",L"",L"",adOpenUnspecified) ;
with this:
C++ Syntax (Toggle Plain Text)
con->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\204504519\\Desktop\\webcam.mdb;Persist Security Info=False","","",adOpenUnspecified);
...and it works perfectly...
Thanks for your help,
Emily
![]() |
Similar Threads
- database connection(select ,insert query) within javascript function (JSP)
- Login Page Database connection to MSAccess (VB.NET)
- Problem with database connection (JSP)
- Database Connection in Visual Studio 2005 (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: Linear search
- Next Thread: Diff between Identifier and keyword
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





