954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Visual C++, Database connection, please help

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:

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

emilyp
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Also I am using a dialog based project.

Thanks
Emily

emilyp
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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=;

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

Hello :cheesy:

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

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


with this:

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

emilyp
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

hi have u got the solution to connect database to visual c++.please forward the solution......i am in need........please.my email id is <>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:

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

iravishekhar
Newbie Poster
1 post since Apr 2010
Reputation Points: 5
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You