Implicit conversion from data type sql_variant to varchar is not allowed

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
alemio
Posts: 12
Joined: Wed 20 May 2015 14:31

Implicit conversion from data type sql_variant to varchar is not allowed

Post by alemio » Thu 21 May 2015 08:32

I'm working with SQL server.
If I try to do an INSERT like this:

TUniSQL *UniSQL = new TUniSQL(NULL);
UniSQL->Connection = UniConnection1;
UniSQL->Debug = true;
UniSQL->ParamCheck = false;
AnsiString text = "INSERT INTO AgeCategories VALUES (:AgeCategoryId, :AgeCategoryName, :LocalizedAgeCategoryName)";
UniSQL->SQL->Clear();
UniSQL->SQL->Add(text);
UniSQL->ParamCheck = true;
UniSQL->Prepared = true;
UniSQL->Params->ParamByName("AgeCategoryId")->AsString = "AA";
UniSQL->Params->ParamByName("AgeCategoryName")->AsString = "TEST";
UniSQL->Params->ParamByName("LocalizedAgeCategoryName")->AsString = AnsiString("a");

UniConnection1->StartTransaction();
try
{
UniSQL->Execute();
UniConnection1->Commit();
}
catch (Exception *E)
{
UniConnection1->Rollback();
throw E;
}

I get an exeption:

Project TestSQLServer.exe raised exception class EUniError with message 'Statement(s) could not be prepared.
Implicit conversion from data type sql_variant to varchar is not allowed. Use the CONVERT function to run this query.'. Process stopped. Use Step or Run to continue.

The table schema is the following:

CREATE TABLE [dbo].[AgeCategories](
[AgeCategoryId] [varchar](2) NOT NULL,
[AgeCategoryName] [nvarchar](50) NOT NULL,
[LocalizedAgeCategoryName] [nvarchar](50) NULL,
CONSTRAINT [PK_AgeCategories] PRIMARY KEY CLUSTERED
(
[AgeCategoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

It works if I execute visually on the form.

Regards

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by azyk » Fri 22 May 2015 12:08

To avoid this error message, you should set the TUniSQL->Prepared property in your code sample to true after you assign values to all the parameters. Taking into account this edit, your code will look like the following:

Code: Select all

...
// UniSQL->Prepared = true; // remove this line
UniSQL->Params->ParamByName("AgeCategoryId")->AsString = "AA";
UniSQL->Params->ParamByName("AgeCategoryName")->AsString = "TEST";
UniSQL->Params->ParamByName("LocalizedAgeCategoryName")->AsString = AnsiString("a");
UniSQL->Prepared = true; // insert code here
...

alemio
Posts: 12
Joined: Wed 20 May 2015 14:31

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by alemio » Fri 22 May 2015 12:17

Thank you, works fine.
Another question: should I use one connection for each thread or just one for all?

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by azyk » Fri 22 May 2015 12:53

A separate TUniConnection instance must be used for each thread in a multi-threaded application.

alemio
Posts: 12
Joined: Wed 20 May 2015 14:31

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by alemio » Fri 22 May 2015 13:17

Thank you.

Just one question

I have a TUniConnection in my c++ form

object UniConnection1: TUniConnection
ProviderName = 'SQL Server'
Database = 'RaceManagerDevelopment'
SpecificOptions.Strings = (
'SQL Server.Authentication=auWindows')
Pooling = True
Server = 'NB-ALESSANDROM\SQLEXPRESS'
Connected = True
LoginPrompt = False
Left = 424
Top = 168
end

And I can connect this component to the sql server database.
But if I do that in the code

TUniConnection* _uniConnection = new TUniConnection(NULL);
_uniConnection->ConnectString= "Provider Name=SQL Server;Login Prompt=False;Pooling=True;Data Source=NB-ALESSANDROM\SQLEXPRESS;Initial Catalog=RaceManagerDevelopment;Port=0;Integrated Security=SSPI";
_uniConnection->SpecificOptions->Add("SQL Server.Authentication=auWindows");
_uniConnection->Connect();

I receive this error

'Named Pipes Provider: Could not open a connection to SQL Server [53]. '

What is wrong?

alemio
Posts: 12
Joined: Wed 20 May 2015 14:31

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by alemio » Fri 22 May 2015 13:20

Thank you, I see now ... should be "NB-ALESSANDROM\\SQLEXPRESS" instead of "NB-ALESSANDROM\SQLEXPRESS"

I took this string from inspector.

Now it works

Thank you again for your support

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Implicit conversion from data type sql_variant to varchar is not allowed

Post by azyk » Mon 25 May 2015 08:40

Glad to see that the issue was resolved. If any other questions come up, please contact us.

Post Reply