Click here to Skip to main content
15,911,646 members
Home / Discussions / Database
   

Database

 
GeneralPassing a var to a SP.. Pin
Jacob Hammack30-Sep-04 15:32
Jacob Hammack30-Sep-04 15:32 
GeneralRe: Passing a var to a SP.. Pin
Christian Graus30-Sep-04 16:30
protectorChristian Graus30-Sep-04 16:30 
GeneralRe: Passing a var to a SP.. Pin
Jacob Hammack30-Sep-04 20:22
Jacob Hammack30-Sep-04 20:22 
GeneralRe: Passing a var to a SP.. Pin
Colin Angus Mackay1-Oct-04 0:26
Colin Angus Mackay1-Oct-04 0:26 
GeneralBeginner OLE question Pin
errenden30-Sep-04 10:01
errenden30-Sep-04 10:01 
GeneralRe: Beginner OLE question Pin
ThomasH13-Oct-04 12:58
ThomasH13-Oct-04 12:58 
GeneralRe: Beginner OLE question Pin
errenden4-Oct-04 5:23
errenden4-Oct-04 5:23 
GeneralRe: Beginner OLE question Pin
ThomasH14-Oct-04 13:04
ThomasH14-Oct-04 13:04 
Beautiful! I use Oracle also. (I also had better luck using Oracle's provider, instead of the Microsoft OLE DB for Oracle provider.)

I've actually used that method (pull a default row)- I take the default "take everything" accessor, and change it into a "get me the row with this primary key" parameterized query. If the accessor returns a row, I know that the data already existed in the table. If the rowset is empty, then I set the values up and update the row. Of course, you can handle this by checking out HRESULT and etc to determine if the row already exists...

You probably want to create a command object instead of a rowset object. That way you can do direct insert/update/deletes, as well as run stored procedures. In fact, you might want to turn your insert into a stored procedure! Either way, at the top of the accessor, add in all your member variables (TCHARs, DOUBLEs, etc) just as if you were dealing with a rowset accessor. In fact, you might already have this if you created a default rowset accessor! Then, define your parameter map...

BEGIN_PARAM_MAP(CNAMEOFACCESSORAccessor)<br />
  COLUMN_ENTRY(1, mFIRSTFIELD)<br />
  COLUMN_ENTRY(2, mSECONDFIELD)<br />
  COLUMN_ENTRY(ETC...)<br />
END_PARAM_MAP()


Make sure you defined mFIRSTFIELD and mSECONDFIELD and etc first.

Now, change up the default "rowset" command that you were given.

DEFINE_COMMAND(CNAMEOFACCESSORAccessor, _T("INSERT INTO MYTABLE VALUES (?, ?)"))

Look down at the in the command class that ATL built for the OpenRowset method. Look for

return CCommand<CAccessor<CNAMEOFACCESSORAccessor>>::Open(mSession);

You've gotta change the CCommand::Open parameters. (Look it up in the MSDN, search for "CCommand::Open".) Leave the first parameter as m_session. You can set the second parameter, szCommand, to NULL because you're using the Accessor for the command. The third parameter would be for a property set, if you defined any. (If you're calling an insert, I'd think you would... I used stored procedures so I can't say for sure, but I think you'd need DBPROP_UPDATABILITY.) The fourth parameter's important, that's a pointer (pRowsAffected) to the number of rows that got DML performed. In your case, this should come back as 1 for a successful insert. (Negative 1 means no rows affected, as does a NULL pointer.)

Hope this helps; or at least gives you something to search the VC++ help files and the web for!

Oh- for the code where I queried first for the data, and if I got no rows back, then I'd insert the data- I sorta cheated. (laughs) I defined an LPCSTR inside of the CCommand called MyQuery. Then, in OpenRowSet, instead of the CCommand<CAccessor<blah blah>>::Open(mSession) I would do CCommand<CAccessor<blah blah>>::Open(mSession, MyQuery) instead.

In my main code, before I did a m_MyObject.Open(), I'd first do m_MyObject.MyQuery = "select these,columns from this.table where pk1=? and pk2=?". (You need ?'s for placeholders.) Then I'd assign values for my m_commandMyObject's parameters- the ones in the param list- like _tcscpy(m_commandMyObject.m_PK1,W2T(BstrVariable)). You've gotta do that for each parameter! Finally I'd do m_commandMyObject.Open(). This would load up my accessor's column map with the row matching pk1 and pk2, OR, it would have null rows. Once it was opened successfully, I'd check the rowset by doing: if (m_MyObject.MoveFirst()==S_OK) then (show an error because the row existed already) else (update rowset). And to update the rowset, all I had to do was set the column values of the accessor, and call m_MyObject.SetData.

This allowed me to work with just one row at a time; fetch the row from the database based on the order number; if the rowset comes back empty, then it's okay to insert the row!

One more thing- if you do decide to use stored procedures, you'll need to add extra lines to your param_map. BEFORE each COLUMN_ENTRY(...) line in the PARAM_MAP, you need to tell the provider which direction that parameter will go. Here's an example, first variable is sent up to Oracle, second variable comes back from Oracle:

BEGIN_PARAM_MAP(CMYTABLEAccessor)<br />
  SET_PARAM_TYPE(DBPARAMIO_INPUT)<br />
  COLUMN_ENTRY(1, mFIRSTFIELD_IN)<br />
  SET PARAM_TYPE(DBPARAMIO_OUTPUT)<br />
  COLUMN_ENTRY(2, mRESULTVAR_OUT)<br />
END_PARAM_MAP()


So I hope all that helps; if you search the Visual Studio help files (or even msdn.microsoft.com) for CCommand::Open , all of this should be more clear. But you're correct, you don't have to do anything with a rowset if you're just inserting data. And you might want to use stored procedures; that way, you let Oracle take care of wondering if a row already exists or not. If you can find it, look for the "Microsoft OLE DB 2.0 Programmer Reference and Data Access SDK" book- it's been out of print for a while, so it's hard to find. To paraphrase every ADO book, "OLE DB is the best for database access. Since it's so hard, we'll use ADO in this book." That killed off the OLE DB books pretty quickly! I found mine on eBay for cheap. The ISBN is 0-7356-0590-4, but the info in the book is still available on msdn.microsoft.com, in the VC++ 6.0 help files, and I just checked my .NET MSDN help files- it's in there, too.

Let me know how it goes! Also, just curious- why are you using OLE DB? It seems like the .NET stuff has become faster than OLE DB; I'm actually transitioning my OLE DB VC++ programming to the .NET Framework. (I'm working right now with C#.NET and ADO.NET, once I get a little better with it, I'll turn it into Managed C++ code.)

Wow, this is one long reply.


-Thomas
GeneralRe: Beginner OLE question Pin
errenden5-Oct-04 3:59
errenden5-Oct-04 3:59 
GeneralTransaction SQL help (SQL Server 2000) Pin
Matt Newman30-Sep-04 6:31
Matt Newman30-Sep-04 6:31 
GeneralRe: Transaction SQL help (SQL Server 2000) Pin
Colin Angus Mackay30-Sep-04 7:19
Colin Angus Mackay30-Sep-04 7:19 
GeneralRe: Transaction SQL help (SQL Server 2000) Pin
Matt Newman30-Sep-04 7:27
Matt Newman30-Sep-04 7:27 
GeneralRe: Transaction SQL help (SQL Server 2000) Pin
Colin Angus Mackay30-Sep-04 9:51
Colin Angus Mackay30-Sep-04 9:51 
GeneralRe: Transaction SQL help (SQL Server 2000) Pin
Matt Newman30-Sep-04 10:31
Matt Newman30-Sep-04 10:31 
GeneralADO memory leak Pin
Droiddr29-Sep-04 23:47
Droiddr29-Sep-04 23:47 
GeneralRe: ADO memory leak Pin
sreejith ss nair7-Oct-04 0:53
sreejith ss nair7-Oct-04 0:53 
GeneralRunning SQL Agent from VB.NET and getting execution status Pin
Emilio Crespo29-Sep-04 6:53
Emilio Crespo29-Sep-04 6:53 
Generalvery strange behaviour Pin
ricardojb29-Sep-04 5:54
ricardojb29-Sep-04 5:54 
GeneralRe: very strange behaviour Pin
sreejith ss nair7-Oct-04 0:51
sreejith ss nair7-Oct-04 0:51 
GeneralStored Procedures and Return Values Pin
Ryan@SalamanderTechnologies29-Sep-04 3:53
sussRyan@SalamanderTechnologies29-Sep-04 3:53 
GeneralRe: Stored Procedures and Return Values Pin
Colin Angus Mackay29-Sep-04 4:41
Colin Angus Mackay29-Sep-04 4:41 
GeneralStored procedure / query optimization Pin
Vagif Abilov28-Sep-04 7:47
professionalVagif Abilov28-Sep-04 7:47 
GeneralRe: Stored procedure / query optimization Pin
Steven Campbell28-Sep-04 8:19
Steven Campbell28-Sep-04 8:19 
GeneralRe: Stored procedure / query optimization Pin
Vagif Abilov28-Sep-04 8:52
professionalVagif Abilov28-Sep-04 8:52 
GeneralRe: Stored procedure / query optimization Pin
Steven Campbell28-Sep-04 9:48
Steven Campbell28-Sep-04 9:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.