ADO.NET provider for ServiceNow / Querying Data
Querying Data

The ADO.NET Provider for ServiceNow implements two classes you can use to perform CRUD (Create, Read, Update, and Delete) operations: DbDataReader and DbDataAdapter classes. The tabs below describe the interfaces and code implementations.

Querying With DbDataReader

The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the DbDataAdapter as it retrieves data in pages. When you read data from the DbDataReader, it requests the succeeding page from the data source to load the result, which makes the data retrieval faster.

The following code examples demonstrate read, create, update, and delete operations from the data source using the DbDataReader.

Create Data

The example adds new records by executing the Insert command.

C#
Copy Code
static void InsertDataReader()
{
    using (C1ServiceNowConnection conn = new C1ServiceNowConnection(connectionString))
    {
        conn.Open();

        string insertSql = @"Insert into incident(caller_id, category, description, close_code) 
    values('781ccaf9c0a8016400b98a06818d57d8','inquiry','I am unable to connect to the data server. It appears to be down.','Solved (Permanently)')";

        //Create Insert command;
        C1ServiceNowCommand command = new C1ServiceNowCommand(conn, insertSql);

        // Execute Insert command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Insert operation successful !!! \n \n");
        }
    }
}

Read Data

The example retrieves data from ServiceNow by executing the Select command.

C#
Copy Code
static void ReadDataReader()
{
    using (C1ServiceNowConnection con = new C1ServiceNowConnection(connectionString))
    {
        con.Open();

        //Create Read command
        var cmd = con.CreateCommand();
        cmd.CommandText = "Select caller_id, category, description, close_code from incident";

        //Execute Read command and display fetched data
        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine("CallerId:{0}", rdr["caller_id"]);
            Console.WriteLine("Category:{0}", rdr["category"]);
            Console.WriteLine("Description:{0}", rdr["description"]);
            Console.WriteLine("CloseCode:{0}", rdr["close_code"]);
            Console.WriteLine("\n");
        }
        Console.WriteLine("Read operation successful !!! \n \n");
    }
}

Update Data

This example modifies data by executing the Update command.

C#
Copy Code
static void UpdateDataReader()
{
    using (C1ServiceNowConnection conn = new C1ServiceNowConnection(connectionString))
    {
        conn.Open();

        //Create Update command               
        C1ServiceNowCommand command = new C1ServiceNowCommand(conn, "UPDATE incident SET close_code=@close_code where caller_id=@caller_id");
        command.Parameters.AddWithValue("@caller_id", "681ccaf9c0a8016400b98a06818d57c7");
        command.Parameters.AddWithValue("@close_code", "Solved Remotely (Permanently)");

        //Execute Update command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Update operation successful !!! \n \n");
        }
    }
}

Delete Data

This example removes data by executing the Delete command.

C#
Copy Code
static void DeleteDataReader()
{
    using (C1ServiceNowConnection conn = new C1ServiceNowConnection(connectionString))
    {
        conn.Open();

        //Create Delete command
        C1ServiceNowCommand command = new C1ServiceNowCommand(conn, "Delete from incident where caller_id = @caller_id");
        command.Parameters.AddWithValue("@caller_id", "781ccaf9c0a8016400b98a06818d57d8");

        //Execute Delete command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Delete operation successful !!! \n \n");
        }
    }
}