ADO.NET Provider for ServiceNow

Build 23.0.8839

ストアドプロシージャの呼び出し

他のSQL ストアドプロシージャと同様に、ServiceNowCommand を使用してストアドプロシージャを呼び出すことができます。ServiceNowCommand オブジェクトをインスタンス化するには、コンストラクタへの引数として、ストアドプロシージャの名前とServiceNowConnection インスタンスを渡します。CommandType プロパティをStoredProcedure に設定し、ServiceNowCommand インスタンスのParameters コレクションにキーと値のペアとしてパラメータを追加します。

C#

string connectionString = "InitiateOAuth=GETANDREFRESH;OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;";

using (ServiceNowConnection connection = new ServiceNowConnection(connectionString)) {
  ServiceNowCommand cmd = new ServiceNowCommand("RefreshOAuthAccessToken", connection);
  cmd.CommandType = CommandType.StoredProcedure; 
  cmd.Parameters.Add(new ServiceNowParameter("@OAuthAccessToken", "OAuthAccessToken"));
  // Add other parameters as needed ...

  ServiceNowDataReader rdr = cmd.ExecuteReader();
  while (rdr.Read()) {
    for (int i = 0; i < rdr.FieldCount; i++) {
      Console.WriteLine(rdr.GetName(i) + " --> " + rdr[i]);
    }
    Console.WriteLine();
  }
}

VB.NET

Dim connectionString As String = "InitiateOAuth=GETANDREFRESH;OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;"

Using connection As New ServiceNowConnection(connectionString)
  Dim cmd As New ServiceNowCommand("RefreshOAuthAccessToken", connection)
  cmd.CommandType = CommandType.StoredProcedure
  cmd.Parameters.Add(New ServiceNowParameter("@OAuthAccessToken", "OAuthAccessToken"))
  ' Add other parameters as needed ...

  Dim rdr As ServiceNowDataReader = cmd.ExecuteReader()
  While rdr.Read()
	  For i As Integer = 0 To rdr.FieldCount - 1
		  Console.WriteLine(rdr.GetName(i) + " --> " + rdr(i))
	  Next
	  Console.WriteLine()
  End While
End Using

別の方法として、ストアドプロシージャのパラメータをコマンドのテキストとして設定することもできます。ストアドプロシージャのステートメントは、次の標準的な形式でサポートされます。

"EXECUTE my_proc @first = 1, @second = 2, @third = 3;"

"EXEC my_proc @first = 1, @second = 2, @third = 3;"

パラメータ化されたクエリを実行するには、ServiceNowCommand インスタンスのParameters コレクションに、キーと値のペアとしてパラメータを追加します。

C#

string connectionString = "InitiateOAuth=GETANDREFRESH;OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;";

using (ServiceNowConnection connection = new ServiceNowConnection(connectionString)) {
  ServiceNowCommand cmd = new ServiceNowCommand("EXECUTE RefreshOAuthAccessToken OAuthAccessToken = @OAuthAccessToken;", connection); 
  cmd.Parameters.Add(new ServiceNowParameter("@OAuthAccessToken", "OAuthAccessToken"));
  // Add other parameters as needed ...

  ServiceNowDataReader rdr = cmd.ExecuteReader();
  while (rdr.Read()) {
    for (int i = 0; i < rdr.FieldCount; i++) {
      Console.WriteLine(rdr.GetName(i) + " --> " + rdr[i]);
    }
    Console.WriteLine();
  }
}

VB.NET

Dim connectionString As String = "InitiateOAuth=GETANDREFRESH;OAuthClientId=MyClientId;OAuthClientSecret=MyClientSecret;User=MyUser;Password=MyPassword;Url=https://MyInstance12345.service-now.com/;"

Using connection As New ServiceNowConnection(connectionString)
  Dim cmd As New ServiceNowCommand("EXECUTE RefreshOAuthAccessToken OAuthAccessToken = @OAuthAccessToken;", connection)
  cmd.Parameters.Add(New ServiceNowParameter("@OAuthAccessToken", "OAuthAccessToken"))
  ' Add other parameters as needed ...

  Dim rdr As ServiceNowDataReader = cmd.ExecuteReader()
  While rdr.Read()
	  For i As Integer = 0 To rdr.FieldCount - 1
		  Console.WriteLine(rdr.GetName(i) + " --> " + rdr(i))
	  Next
	  Console.WriteLine()
  End While
End Using

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839