🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.database.dbexpress
Article #182

TSQLQuery - ParamByName and the AsDateTime

#182
From: Ned
Date: Fri, 25 Jan 2008 02:03
83 lines
4001 bytes
Hi,

Not sure if this has been covered anywhere in the newsgroups. Couldn't see 
anything. I spent the last day trying to see why a number of BCB 6 project we 
had no longer work when the parameters are set in a query.

I found that calling ParmByName("paramNameToSet")->AsDateTime = aDateTimeObject, 
did not actually set the parameter when ExecSQL is called. The parameter appears 
to be set when you check it but it is not passed to the SQL Server. The error 
shown is

... raised exception class EDatabaseError with message 'No Value for parameter 
'pEndDate".

I am using SQL Server 2005 and the enterprise supplied DBExpress dbxmss30.dll 
driver.

It all works if the aDateTimeObject above is converted to an AnsiString and the 
AsDateTime call is replaced with AsString. Both options shown below in code.


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "DatabaseAccess.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define USE_TDATETIME

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
//  Connect to the database using the built in SQL Enterprise driver
//
	dbProduction->ConnectionName = "MSSQLConnection";
	dbProduction->DriverName     = "MSSQL";
	dbProduction->LoginPrompt    = false;
	dbProduction->GetDriverFunc  = "getSQLDriverMSSQL";
	dbProduction->LibraryName    = "dbxmss30.dll";
	dbProduction->VendorLib      = "oledb";
	dbProduction->Params->Add("HostName=");
	dbProduction->Params->Values["HostName"] = "SQLExpress\\SQLExpress";
	dbProduction->Params->Values["User_Name"] = "userName";
	dbProduction->Params->Values["Password"]  = "password";
	dbProduction->Params->Values["DataBase"]  = "Orders";
	dbProduction->Open();
//
//  Test the query operation with TDateTime
//
    AnsiString sParam[5];
    float fParam[5];
    TDateTime currentTime(Now());
    AnsiString endDate;
    AnsiString startDate;

    endDate   = AnsiString(currentTime.DateTimeString());
    startDate = AnsiString(currentTime.DateTimeString());

#ifdef USE_TDATETIME
    qryAddProduction->Prepared = true;
    qryAddProduction->ParamByName("pEndDate")->AsDateTime   = currentTime;
    qryAddProduction->ParamByName("pStartDate")->AsDateTime = currentTime;
#else
    qryAddProduction->ParamByName("pEndDate")->AsString   = endDate;
    qryAddProduction->ParamByName("pStartDate")->AsString = startDate;
#endif

    qryAddProduction->ParamByName("pOrderNo")->AsString          = "99999";
    qryAddProduction->ParamByName("pInputOrderNo")->AsString     = "99999";
    qryAddProduction->ParamByName("pCompleted")->AsInteger       = false;
    qryAddProduction->ExecSQL();
}
//---------------------------------------------------------------------------
Technical Details
Message-ID: <4799437b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news2.euro.net!feeder.news-service.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: fb4e2,165e6b8d6551d94c,start
X-Google-Attributes: gidfb4e2,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 25 Jan 2008 13:03:40 +1100
From: Ned <nemanada@yahoo.com>
User-Agent: Thunderbird 2.0.0.9 (Windows/20071031)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.database.dbexpress
Subject: TSQLQuery - ParamByName and the AsDateTime 
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 125.255.47.140
Message-ID: <4799437b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1201226619 125.255.47.140 (24 Jan 2008 18:03:39 -0700)
Lines: 83
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news2.euro.net!feeder.news-service.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail