Jaa


How to Delete Users Based on Criteria

To bulk-delete users in your site code, you use the OLEDB Provider for Commerce Server Core Systems, also known as the CSOLEDB provider. To use the CSOLEDB provider, you run a SQL query to bulk-delete profiles based on the criteria that you specify in the query.

To delete profiles

  1. Create a string that contains the SQL statement to run through the CSOLEDB provider.

    Note

    Your statement must comply with the SQL query syntax accepted by the CSOLEDB provider. For more information about the correct SQL query syntax to use, see SQL Queries Supported by the OLE DB Provider for Commerce Server.

  2. Create a ProfileContext object to attach to the Commerce Server Core Systems  runtime.

  3. Create a new RecordsetClass object.

  4. Open a new record set by opening a connection to the CSOLEDB provider. This connection that is exposed by the CommerceContext object.

Example

The following code example shows how to delete profiles that are inactive. Inactive profiles have an account_status value of 0. For more information about the account_status field, see the UpmMembershipUserAccountStatus class.

public static void BulkDeleteInactiveAccounts()
{
    // Construct a SQL string.
    string cmdText = "DELETE FROM UserObject WHERE AccountInfo.account_status = '0'";

    // Get the Profiles run-time object.
    ProfileContext ctxt = CommerceContext.Current.ProfileSystem;

    // Create a new RecordsetClass object.
    ADODB.RecordsetClass rs = new ADODB.RecordsetClass();
    try
    {
        // Open a RecordsetClass by running the SQL statement to the CSOLEDB provider.
        rs.Open(
            cmdText,
            ctxt.CommerceOleDbProvider,
            ADODB.CursorTypeEnum.adOpenForwardOnly,
            ADODB.LockTypeEnum.adLockReadOnly,
            (int)ADODB.CommandTypeEnum.adCmdText);
    }
    finally
    {
        Marshal.ReleaseComObject(rs);
    }
}

Compiling the Code

To compile the code, you must include the following namespace directives:

using System;
using System.Runtime.InteropServices;
using Microsoft.CommerceServer.Profiles;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;

See Also

Other Resources

Profiles System and OLE DB Provider

Developing with the Profiles System