Visual C++, Database connection, please help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 6
Reputation: emilyp is an unknown quantity at this point 
Solved Threads: 0
emilyp's Avatar
emilyp emilyp is offline Offline
Newbie Poster

Visual C++, Database connection, please help

 
-1
  #1
Dec 6th, 2006
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:
  1. CDialog::EnableConnections();
  2. ::CoInitialize(NULL);
  3.  
  4. _ConnectionPtr con;
  5. con.CreateInstance(__uuidof(Connection));
  6.  
  7. con->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\Documents and Settings\\204504519\\Desktop\\webcam.mdb",_bstr_t (""),_bstr_t(""),adOpenUnspecified);
  8.  
  9.  
  10. _CommandPtr cmd;
  11. cmd.CreateInstance(__uuidof(Command));
  12. cmd->ActiveConnection = con;
  13. cmd->CommandText = "Select * from Main";
  14. _RecordsetPtr pRs;
  15.  
  16. pRs.CreateInstance(__uuidof(Recordset));
  17. pRs ->putref_Source(cmd);
  18.  
  19.  
  20. _variant_t vNull;
  21. vNull.vt = VT_ERROR;
  22. vNull.scode = DISP_E_PARAMNOTFOUND;
  23. pRs->Open(vNull,vNull,adOpenDynamic,adLockOptimistic,adCmdUnknown);
  24.  
  25.  
  26. COleSafeArray vaFieldlist;
  27. vaFieldlist.CreateOneDim(VT_VARIANT,6);
  28. long lArrayIndex[1];
  29.  
  30. lArrayIndex[0] = 0;
  31. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("UserName")));
  32. lArrayIndex[0] = 1;
  33. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Name")));
  34. lArrayIndex[0] = 2;
  35. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Surname")));
  36. lArrayIndex[0] = 3;
  37. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("IDNum")));
  38. lArrayIndex[0] = 4;
  39. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("Password")));
  40. lArrayIndex[0] = 5;
  41. vaFieldlist.PutElement(lArrayIndex,&(_variant_t("UserType")));
  42.  
  43. COleSafeArray vaValuelist;
  44. vaValuelist.CreateOneDim(VT_VARIANT,6);
  45. lArrayIndex[0] = 0;
  46. vaValuelist.PutElement(lArrayIndex,&(_variant_t("")));
  47. lArrayIndex[0] = 1;
  48. vaValuelist.PutElement(lArrayIndex,&(_variant_t("")));
  49. lArrayIndex[0] = 2;
  50. vaValuelist.PutElement(lArrayIndex,&(_variant_t("")));
  51. lArrayIndex[0] = 3;
  52. vaValuelist.PutElement(lArrayIndex,&(_variant_t("")));
  53. lArrayIndex[0] = 4;
  54. vaValuelist.PutElement(lArrayIndex,&(_variant_t("")));
  55. lArrayIndex[0] = 5;
  56. vaValuelist.PutElement(lArrayIndex,&(_variant_t("Client")));
  57.  
  58. pRs->AddNew(vaFieldlist,vaValuelist);
  59. UpdateData(TRUE);
  60. _variant_t vName,vVal;
  61. vName.SetString("UserName");
  62. vVal.SetString(m_UName);
  63. pRs->Update(vName,vVal);
  64. vName.SetString("Name");
  65. vVal.SetString(m_Name);
  66. pRs->Update(vName,vVal);
  67. vName.SetString("Surname");
  68. vVal.SetString(m_Surname);
  69. pRs->Update(vName,vVal);
  70. vName.SetString("IDNum");
  71. vVal.SetString(m_IDNum);
  72. pRs->Update(vName,vVal);
  73. vName.SetString("Password");
  74. vVal.SetString(m_Password);
  75. pRs->Update(vName,vVal);
  76. vName.SetString("UserName");
  77. vVal.SetString(m_UName);
  78. pRs->Update(vName,vVal);
  79. vName.SetString("UserType");
  80. vVal.SetString(m_UType);
  81. pRs->Update(vName,vVal);
  82.  
  83.  
  84. pRs->Close();
  85. con->Close();
  86. CoUninitialize();
  87.  
  88. CDialog::OnOK();
  89. }
  90.  
  91.  
  92. void CNew::OnCancel()
  93. {
  94. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 6
Reputation: emilyp is an unknown quantity at this point 
Solved Threads: 0
emilyp's Avatar
emilyp emilyp is offline Offline
Newbie Poster

Re: Visual C++, Database connection, please help

 
0
  #2
Dec 6th, 2006
Also I am using a dialog based project.

Thanks
Emily
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Visual C++, Database connection, please help

 
0
  #3
Dec 7th, 2006
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=;
Last edited by Killer_Typo; Dec 7th, 2006 at 1:16 am.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 6
Reputation: emilyp is an unknown quantity at this point 
Solved Threads: 0
emilyp's Avatar
emilyp emilyp is offline Offline
Newbie Poster

Re: Visual C++, Database connection, please help

 
0
  #4
Dec 7th, 2006
Hello :cheesy:

I've got the code working. I replaced the code:

  1.  
  2. con->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\204504519\\Desktop\\webcam.mdb",L"",L"",adOpenUnspecified) ;

with this:
  1.  
  2. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC