Trying to retreiving all data from a truncated string
-
2 mai 2012 12:11
I have a generic function that reads data from tables without knowing the maximum data length.
This should be fairly streight forward but I cannot get it to work.
My application is written in C++ using SQL Native Client 10.0.
I have already established the DB connect and perform the following to get to my data:
char* m_pszText; char m_szString[255]; SQLLEN StrLen_or_Ind; sqlRetCode = SQLAllocHandle( SQL_HANDLE_STMT, m_hdbc, &m_hstmt ); sqlRetCode = SQLPrepare( m_hstmt, (SQLTCHAR *) "SELECT StringColumn from table", SQL_NTS ); sqlRetCode = SQLBindCol( m_hstmt, (SQLUSMALLINT) 1, SQL_C_CHAR, m_szString, 255, &StrLen_or_Ind ); sqlRetCode = SQLExecute( m_hstmt ); do { sqlRetCode = SQLFetch( m_hstmt ); if( sqlRetCode == SQL_NO_DATA ) { m_bEOF = TRUE; return; } if ( sqlRetCode == SQL_SUCCESS_WITH_INFO ) { SDWORD nErr; TCHAR szSQLSTATE[10]; SWORD cbms if( ( sqlRetCode = SQLGetDiagRec( SQL_HANDLE_STMT, m_hstmt, 1, (SQLTCHAR*) szSQLSTATE, &nErr, NULL, 0, &cbmsg ) ) == SQL_SUCCESS && _tcscmp( szSQLSTATE, _T("01004") ) == 0 ) { m_pszText = (char*) malloc( StrLen_or_Ind + 1 ); sqlRetCode = SQLBindCol( m_hstmt, (SQLUSMALLINT) 1, SQL_C_CHAR, m_pszText, StrLen_or_Ind + 1, StrLen_or_Ind ); // NOW HOW DO I GET m_pszText LOADED WITH THE FULL DATA STRING?????????????? } } } while( 1 );Actually, when I try to free m_pszText, I get: 0xC0000005: Access violation reading location 0x00000000
- Editat de HerveGagnon 2 mai 2012 12:13
- Editat de HerveGagnon 2 mai 2012 12:14
Toate mesajele
-
2 mai 2012 14:24hmm, I'm not sure what you're trying to do with the second SQLBindCol. According to the documentation of SQLBindCol, the value should already be available in m_szString. btw, you shoud use SQL_C_CHAR as type in the first SQLBindCol call.
- Editat de Stefan HoffmannMVP 2 mai 2012 14:27
-
2 mai 2012 19:24According to the documentation, I need to rebind the column with the new increased buffer. The m_szString only contains 255 characters. The first SQLBindCol is a SQL_C_CHAR.
-
3 mai 2012 06:37I see. You need either SQLColAttribute or SQLDescribeCol to retrieve the maximum size before binding.
- Propus ca răspuns de Papy NormandModerator 10 mai 2012 20:55
- Anulare propunere ca răspuns de Papy NormandModerator 10 mai 2012 20:57
-
3 mai 2012 11:10
Other alternative could be to increase the size of buffer. Instead of SQLBindCol you could have use SQLGetData as well to retreive the data
regards,
Vatsa
-
3 mai 2012 20:02
Hi Stefan, thanks for the info. I don't actually need to perform an SQLColAttribute or SQLDescribeCol because the real length is already available in the field StrLenOrInd which is set by SQLFetch().
Hi vatsa, yes I have been comtemplating using SQLGetData instead of SQLBindCol. I was worried about system performance. Do you know which method is most efficient and/or flexible?
- Editat de HerveGagnon 3 mai 2012 20:10
- Editat de HerveGagnon 3 mai 2012 20:11
-
7 mai 2012 11:44
To be very frank, I never measured performance.However, I have used SQLGetData and it worked really well with my code. I have used SQLBindCol to bind SPs parameters.
below link answers your question:
http://msdn.microsoft.com/en-us/library/ms131269.aspx
regards,
Vatsa
- Marcat ca răspuns de Iric WenModerator 14 mai 2012 07:17