Microsoft KB Archive/310207

= How to use ADO and the Exchange 2000 OLE DB provider to search for items in a folder by using Visual C# =

Article ID: 310207

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft Exchange Server 2000 Service Pack 1
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q310207



Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:

840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



SUMMARY
This article describes how to use ActiveX Data Objects (ADO) and the Microsoft Exchange 2000 Server OLE DB provider to search for items in a folder by using Microsoft Visual C# .NET or Microsoft Visual C# 2005.



MORE INFORMATION
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Console Application. By default, the Class1.cs file is created.

Note In Microsoft Visual C# 2005, click Visual C#. By default, the Program.cs file is created. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate Microsoft ActiveX Data Objects 2.5 Library, and then click Select.

Note Microsoft Visual C# 2005, you do not have to click Select.</li> In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the code window, replace the code with the following: using System;

namespace Samples {   class Class1 {       static void Main(string[] args) {           try {           ADODB.Connection oCn = new ADODB.Connection; ADODB.Recordset oRs = new ADODB.Recordset;

ADODB.Fields oFields; ADODB.Field oField;

// TODO: Replace with your folder's URL. string sFdUrl = &quot;http://ExchServer/Exchange/UserAlias/Inbox&quot;; oCn.Provider = &quot;exoledb.datasource&quot;; oCn.Open(sFdUrl, &quot;&quot;, &quot;&quot;, -1);

if(oCn.State == 1) {               Console.WriteLine(&quot;Good Connection&quot;); }           else {               Console.WriteLine(&quot;Bad Connection&quot;); }

string strSql; strSql = &quot;&quot;; strSql = &quot;select &quot;; strSql = strSql + &quot; \&quot;urn:schemas:mailheader:content-class\&quot;&quot;; strSql = strSql + &quot;, \&quot;DAV:href\&quot; &quot;; strSql = strSql + &quot;, \&quot;DAV:displayname\&quot;&quot;; strSql = strSql + &quot; from scope ('shallow traversal of &quot; + &quot;\&quot;&quot;;           strSql = strSql + sFdUrl + &quot;\&quot;') &quot;; strSql = strSql + &quot; WHERE \&quot;DAV:ishidden\&quot; = false&quot;; strSql = strSql + &quot; AND \&quot;DAV:isfolder\&quot; = false&quot;;

oRs.Open(strSql, oCn,               ADODB.CursorTypeEnum.adOpenStatic,                ADODB.LockTypeEnum.adLockUnspecified, 1);

if(oRs.State == 1) {               Console.WriteLine(&quot;Recordset Opened&quot;); }           else {               Console.WriteLine(&quot;Recordset Failed Opened&quot;); }

oRs.MoveFirst; while(!oRs.EOF) {               oFields = oRs.Fields; oField = oFields[&quot;DAV:href&quot;]; Console.WriteLine(oField.Value);

oField = oFields[&quot;DAV:displayname&quot;]; Console.WriteLine(oField.Value);

oRs.MoveNext; Console.WriteLine(&quot;--&quot;);

}

oRs.Close; oCn.Close;

oCn = null; oRs = null; oFields = null; oField = null; }           catch (Exception e)            { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }                  }        } }                    </li> Search for the TODO text string in the code, and then modify the code for your environment.</li> Press the F5 key to build and to run the program.</li></ol>

Additional query words: search items ADO CDOEX

Keywords: kbhowto kbmsg kbxml kbcode KB310207

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.