Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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 (http://support.microsoft.com/kb/840667/ ) You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

↑ Back to the top


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.

↑ Back to the top


More information

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. 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.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. 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 = "http://ExchServer/Exchange/UserAlias/Inbox";
    			
    			oCn.Provider = "exoledb.datasource";
    			oCn.Open(sFdUrl, "", "", -1);  
    
    			if(oCn.State == 1)
    			{
    				Console.WriteLine("Good Connection");
    			}
    			else
    			{
    				Console.WriteLine("Bad Connection");
    			}
    
    
    			string strSql;
    			strSql = "";
    			strSql = "select ";
    			strSql = strSql + " \"urn:schemas:mailheader:content-class\"";
    			strSql = strSql + ", \"DAV:href\" ";
    			strSql = strSql + ", \"DAV:displayname\"";
    			strSql = strSql + " from scope ('shallow traversal of " + "\"";
    			strSql = strSql + sFdUrl + "\"') ";
    			strSql = strSql + " WHERE \"DAV:ishidden\" = false";
    			strSql = strSql + " AND \"DAV:isfolder\" = false";
    
    
    			oRs.Open(strSql, oCn,
    				ADODB.CursorTypeEnum.adOpenStatic,
    				ADODB.LockTypeEnum.adLockUnspecified, 1);
    
    
    			if(oRs.State == 1)
    			{
    				Console.WriteLine("Recordset Opened");
    			}
    			else
    			{
    				Console.WriteLine("Recordset Failed Opened");
    			}
    
    			oRs.MoveFirst();
    			while(!oRs.EOF)
    			{
    				oFields = oRs.Fields;
                                        oField = oFields["DAV:href"];
    				Console.WriteLine(oField.Value);
    
    				oField = oFields["DAV:displayname"];
    				Console.WriteLine(oField.Value);
    
    				oRs.MoveNext();  
    				                             Console.WriteLine("--------------------------");
    
    			}
    
    			oRs.Close();
    			oCn.Close();
    
    			oCn = null;
    			oRs = null;
    			oFields = null;
    			oField = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
            }
    }
    					
  6. Search for the TODO text string in the code, and then modify the code for your environment.
  7. Press the F5 key to build and to run the program.

↑ Back to the top


Keywords: KB310207, kbcode, kbxml, kbmsg, kbhowto

↑ Back to the top

Article Info
Article ID : 310207
Revision : 6
Created on : 11/29/2007
Published on : 11/29/2007
Exists online : False
Views : 347