0

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:

  1. Create a new VCL project
  2. Drop a TADOConnection and TADOQuery on the form
  3. Setup the TADOConnection to attach to a SQL Server database
  4. Set the .Connection property of the TADOQuery to the TADOConnection
  5. 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:

Message Box

Error 1

Error 2

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?

James L.
  • 9,384
  • 5
  • 38
  • 77
  • I have a vague recollection of reading about problems with XML fields. Does the problem remain if you make the XML column the last one in the table? – MartynA Jul 27 '16 at 08:40
  • @whosrdaddy - Don't know why my searches didn't find that. Thank you! If you want to post as an answer, I'll mark it correct. – James L. Jul 27 '16 at 15:21
  • No problem, no need for an answer as it is an exact dupe ;) – whosrdaddy Jul 27 '16 at 15:23

0 Answers0