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 delete mail items by using an ADO recordset in Visual C#


View products that this article applies to.

This article was previously published under Q310202
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


Introduction

This article describes how to use ActiveX Data Objects (ADO) to delete objects by using an ADO recordset in Microsoft Visual C# . It includes a step-by-step procedure that shows you how to delete a mail item.

Delete a mail item by using an ADO recordset

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New and then click Project.
  3. Under Project Types, click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Under Templates, double-click Console Application.

    By default, Class1.cs is created.

    Note: In Visual Studio 2005, by default, Program.cs is created.
  5. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. In the Add References dialog box, click the COM tab, click Microsoft ActiveX Data Objects 2.5 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    3. Click OK to accept your selections.

      If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  6. In the code window, replace all the code with the following code:
    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 the folder URL with your folder URL.
    				string sFdUrl = "http://ExchServer/exchange/UserAlias/Inbox";
    			
    				oCn.Provider = "exoledb.datasource";
    				oCn.Open(sFdUrl, "", "", -1);  
    
    
    				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";
    				//TODO: Replace 'Test.eml' with the name of the mail that you want to delete.
    				strSql = strSql + " AND \"DAV:displayname\" = 'Test.eml'";  
    
    
    				oRs.Open(strSql, oCn,
    					ADODB.CursorTypeEnum.adOpenUnspecified,
    					ADODB.LockTypeEnum.adLockOptimistic,
    					1);
    
    				Console.WriteLine(oRs.RecordCount);	
       
    				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.Delete(ADODB.AffectEnum.adAffectCurrent);
    
    					oRs.MoveNext();  
    					Console.WriteLine("Item Deleted");
    					Console.WriteLine("--------------------------");
    
    				}
    
    				oCn.Close();
    
    				oCn = null;
    				oField = null;
    				oFields = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
    	}
    }
    

  7. Modify the code accordingly where you see the TODO comment.
  8. Press F5 to build and to run the program.
  9. Verify that the objects were deleted from the folder.

↑ Back to the top


Keywords: KB310202, kbhowtomaster, kbcode, kbxml, kbmsg

↑ Back to the top

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