When I use use Delphi XE2 or XE10 and ADO to pull data from SQL Server 2008 R2 using the SQL Server Native Client 10.0 provider, XML fields are lost and any fields that come after the XML field are inaccessible. To illustrate:
- Create a new VCL project
- Drop a
TADOConnection
andTADOQuery
on the form - Setup the
TADOConnection
to attach to a SQL Server database - Set the
.Connection
property of theTADOQuery
to theTADOConnection
- Drop a button on the form and set its
OnClick()
event to be:
procedure TForm1.Button1Click(Sender: TObject);
begin
ADOConnection1.Open;
try
ADOQuery1.SQL.Text := Concat(
'create table #temp(str1 varchar(255), xml_data xml, str2 varchar(255)) ',
'insert into #temp values (''string 1'', ''<data>xml</data>'', ''string 2'') ',
'select * from #temp ',
'drop table #temp'
);
ADOQuery1.Open;
ShowMessage(ADOQuery1.FieldByName('str1').AsString);
try
ShowMessage(ADOQuery1.FieldByName('xml_data').AsString);
except
on e: Exception do
ShowException(e, ExceptAddr); // trap error and show extra error info
end;
try
ShowMessage(ADOQuery1.FieldByName('str2').AsString);
except
on e: Exception do
ShowException(e, ExceptAddr); // show extra error info
end;
finally
ADOConnection1.Close;
end;
end;
My connection string for the TADOConnection
is Provider=SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=tempdb;Data Source=localhost;Initial File Name="";Server SPN=""
, all other properties of TADOConnection
and TADOQuery
are the defaults when you drop them on the form.
When I run the app and click Button 1, I get one message box and two errors:
The XML field (xml_data
) is not found. And all fields that come after the XML field always return Multi-step operations generated errors. Check each status value. (even if I don't try to access the xml_data
field).
I tried using Microsoft OLE DB Provider for SQL Server and it fixes the issue. However, I have read elsewhere that the OLE DB provider has issues with date fields, so I want to use the SQL Server Native Client 10.0 provider if possible.
What is the trick to get ADO to receive a XML field from the database so that it is properly defined in TADOQuery.FieldDefs
when using SQL Server Native Client 10.0?