Microsoft KB Archive/310202

= How to delete mail items by using an ADO recordset in Visual C# =

Article ID: 310202

Article Last Modified on 11/29/2007

-

APPLIES TO


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

-



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 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



IN THIS TASK

 * INTRODUCTION
 * Delete a mail item by using an ADO recordset



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.

back to the top

Delete a mail item by using an ADO recordset
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New and then click Project. Under Project Types, click Visual C# Projects.

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

By default, Class1.cs is created.

Note: In Visual Studio 2005, by default, Program.cs is created. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do this, follow these steps:  On the Project menu, click Add Reference.</li> 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.</li> Click OK to accept your selections.

If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  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 = &quot;http://ExchServer/exchange/UserAlias/Inbox&quot;; oCn.Provider = &quot;exoledb.datasource&quot;; oCn.Open(sFdUrl, &quot;&quot;, &quot;&quot;, -1);

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;; //TODO: Replace 'Test.eml' with the name of the mail that you want to delete. strSql = strSql + &quot; AND \&quot;DAV:displayname\&quot; = 'Test.eml'&quot;;

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[&quot;DAV:href&quot;]; Console.WriteLine(oField.Value);

oField = oFields[&quot;DAV:displayname&quot;]; Console.WriteLine(oField.Value); oRs.Delete(ADODB.AffectEnum.adAffectCurrent);

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

}

oCn.Close;

oCn = null; oField = null; oFields = null; }           catch (Exception e)            { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }                  }    } }

</li> Modify the code accordingly where you see the TODO comment.</li> Press F5 to build and to run the program.</li> Verify that the objects were deleted from the folder.</li></ol>

back to the top

Additional query words: ADO delete ADO Recordset

Keywords: kbmsg kbxml kbcode kbhowtomaster KB310202

-

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

© Microsoft Corporation. All rights reserved.