Junk varchar entries in MSSQL database using ODBC

160 Views Asked by At

I'm trying to insert a string-variable into a varchar(100)-field, but if the string is longer than 15 elements only junk is inserted (e.g. "0‰?").

First my setup:

Development: Win7 (64bit) / VS2013 / C++11 / 64bit Application

Database: Win8 (64bit) / Microsoft SQL Server Express 2014 (64bit)

Driver: SQL Server Native Client 11.0

Second the binding of the paramter:

std::string mMessageText;
SQLHANDLE mSqlStatementHandle;
std::string mExecString;

bool initConnection()
{
   mExecString = "{? = CALL dbo.InsertTestProcedure(?, ?, ?, ?, ?)}";

   (...)

   // bind parameters
   SQLBindParameter(mSqlStatementHandle, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 100, 0, (SQLPOINTER)mMessageText.c_str(), mMessageText.length(), NULL);

   (...)

   // prepare handle with execution string
   if (SQL_SUCCESS != SQLPrepare(mSqlStatementHandle, (SQLCHAR*)mExecString.c_str(), (SQLSMALLINT)mExecString.length()))
   {
      throwError(SQL_HANDLE_STMT, mSqlStatementHandle);
      return false;
   }
}

Third the query execution:

bool fillDb()
{
   (...)

   mMessageText = "This text is longer than 15";

   // execute SQL statement
   if (SQL_SUCCESS != SQLExecute(mSqlStatementHandle))
   {
      throwError(SQL_HANDLE_STMT, mSqlStatementHandle);
      return false;               
   }

   (...)
}

Header of the procedure:

ALTER PROCEDURE [dbo].[InsertTestProcedure]
 @MessageComp VARCHAR(20),
 @MessageType VARCHAR(20),
 @MessageAction VARCHAR(20),
 @MessageText VARCHAR(100),
 @MessageName VARCHAR(20)
AS

If the string is shorter than 15 elements, it works fine. And calling the procedure from SQL Management Studio with value lengths > 15 works fine too.

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks to @erg, here is the solution that worked for me:

char mMessageText[100];

bool initConnection()
{
   (...)

   // bind parameters
   SQLBindParameter(mSqlStatementHandle, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 100, 0, (SQLPOINTER)mMessageText, 100, NULL);

   (...)
}

bool fillDb()
{
   (...)

   std::string lMessageText = "This text is longer than 15";

   strcpy(mMessageText, lMessageText.c_str());
   mMessageText[sizeof(mMessageText) - 1] = 0;

   (...)
}
2
On

One thing that comes to my mind is the procedure you are calling. Maybe you have table with varchar(100) column, but the procedure has only varchar(15) parameter. Could you post header of that procedure?