0

I am using Visual Studio 2008, and SQL Server, Currently I have a connection using ADO, but would like to retrieve the number of columns a table has...

Here is what I have

/*CODE FOR connecting to database**/

/*query to know number of columns in table*/
_bstr_t sSQLd="Select Count(*) From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME='[dbo].[mytable]';";


/*define variable to hold number of columns and 
print number of coulms as an integer
*/
int COLS_PER_ROW = wcstod(sSQLd, NULL);
printf("COLS_PER_ROW: %d", COLS_PER_ROW);

It prints 0 although There are 20 columns in my table How do I fix the query or the logic??

edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • 2
    I don't see you executing that SQL; you appear to be attempting to convert the query itself to a number. It's gonna be zero. – Roger Lipscombe Jul 16 '11 at 17:00
  • @Roger Lipscombe: How do I add that, what instruction must be added in order to exec the `count` with `Command.Execute ()`?? – edgarmtze Jul 16 '11 at 17:02

1 Answers1

1

I think the examples in this MSDN article will help: How to: Convert Between Various String Types

Quoting from the article:

The strings types that are covered include char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_string, and System.String.

Convert from _bstr_t to wchar_t* or char* and then use strtol()/wcstol() to convert it to long.

yasouser
  • 5,113
  • 2
  • 27
  • 41
  • I was searching in the link you provided, and found out that there is no direct way to convert `_bstr_t` to `int`, what steps do you recommend to do that transform ? maybe using `atoi()` – edgarmtze Jul 16 '11 at 16:42
  • If you have Boost installed, I would suggest using `boost::lexical_cast<>` (http://www.boost.org/doc/libs/1_47_0/libs/conversion/lexical_cast.htm) or else use `strtol()` (http://msdn.microsoft.com/en-us/library/w4z2wdyc%28v=vs.71%29.aspx) – yasouser Jul 16 '11 at 16:50