データベースレコードの取得

You can use a SELECT statement to retrieve records from the database via a SELECT command. To execute a SELECT statement you must:

  • データベース接続を作成して開きます。

  • SELECT ステートメントを表す EDBCommand オブジェクトを作成します。

  • EDBDataReader を返す EDBCommand オブジェクトの ExecuteReader() メソッドでコマンドを実行します

  • Loop through the EDBDataReader displaying the results or binding the EDBDataReader to some control.

An EDBDataReader object represents a forward-only and read-only stream of database records, presented one record at a time. To view a subsequent record in the stream, you must call the Read() method of the EDBDataReader object.

次の例:

  1. AdvancedServerネームスペースをインポートします:EnterpriseDB.EDBClient

  2. SELECT ステートメントで EDBCommand オブジェクトを初期化します。

  3. データベースへの接続を開きます。

  4. Executes the EDBCommand by calling the ExecuteReader method of the EDBCommand object.

SQLステートメントの結果は EDBDataReader オブジェクトに取得されます。

  1. EDBDataReader オブジェクトの内容をループして、クエリによって返されたレコードを WHILE ループ内に表示します。

Read() メソッドは次のレコードに進み(レコードが存在する場合)、レコードが存在する場合は true を返し、 EDBDataReader が末尾に達したことを示す場合は false を返します結果セット。

<% @ Page Language="C#" %>
<% @Import Namespace="EnterpriseDB.EDBClient" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Configuration" %>

<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e)
{
      string strConnectionString = ConfigurationSettings.AppSettings
      ["DB_CONN_STRING"];
      EDBConnection conn = new EDBConnection(strConnectionString);

      try
      {
              conn.Open();
              EDBCommand cmdSelect = new EDBCommand("SELECT * FROM dept",conn);
              cmdSelect.CommandType = CommandType.Text;
              EDBDataReader drDept = cmdSelect.ExecuteReader();

              while(drDept.Read())
              {
                      Response.Write("Department Number: " + drDept["deptno"]);
                      Response.Write("\tDepartment Name: " + drDept["dname"]);
                      Response.Write("\tDepartment Location: " + drDept["loc"]);
                      Response.Write("<br>");
              }

      }

      catch(Exception exp)
{
              Response.Write(exp.ToString());
      }
      finally
{
              conn.Close();
      }
}
</script>

サンプルコードを実行するには、次の名前のファイルのデフォルトのWebルートディレクトリにコードを保存します。

selectEmployees.aspx

プログラムを呼び出すには、Webブラウザーを開き、以下を参照します。

http://localhost/selectEmployees.aspx

単一のデータベースレコードの取得

クエリから単一の結果を取得するには、 EDBCommand オブジェクトの ExecuteScalar() メソッドを使用します。 ExecuteScalar() メソッドは、指定されたクエリによって生成された DataSet の最初の行の最初の列の最初の値を返します。

<% @ Page Language="C#" %>
<% @Import Namespace="EnterpriseDB.EDBClient" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Configuration" %>

<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e)
{
      string strConnectionString = ConfigurationSettings.AppSettings
      ["DB_CONN_STRING"];
      EDBConnection conn = new EDBConnection(strConnectionString);
      try
  {
       conn.Open();
       EDBCommand cmd = new EDBCommand("SELECT MAX(sal) FROM emp",conn);

       cmd.CommandType = CommandType.Text;

       int maxSal = Convert.ToInt32(cmd.ExecuteScalar());

       Response.Write("Emp Number: " + maxSal);

      }
      catch(Exception exp)
  {
    Response.Write(exp.ToString());
      }
      finally
  {
    conn.Close();
      }
}
</script>

次の名前のWebルートディレクトリのファイルにサンプルコードを保存します。

selectscalar.aspx

サンプルコードを呼び出すには、Webブラウザーを開き、以下を参照します。

http://localhost/selectScalar.aspx

サンプルには ExecuteScalar() メソッドによって返された値の明示的な変換が含まれていることに注意してください。 ExecuteScalar() メソッドはオブジェクトを返します。オブジェクトを表示するには、 Convert.ToInt32 メソッドを使用してオブジェクトを整数値に変換する必要があります。