ADO.NET provider for Dynamics 365 Sales > データのクエリ |
The ADO.NET Provider for Dynamics 365 Sales implements two classes you can use to perform CRUD (Create, Read, Update, and Delete) operations: DbDataReader and C1D365SDataAdapter classes. The tabs below describe the interfaces and code implementations.
The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the C1D365SDataAdapter 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 create, read, update, and delete operations from the data source using the DbDataReader.
The example adds new records by executing the Insert command.
C# |
コードのコピー
|
---|---|
static void InsertDataReader() { using (var con = new C1D365SConnection(connectionString)) { con.Open(); // Insert コマンドを作成します。 var cmd = con.CreateCommand(); cmd.CommandText = $" INSERT INTO accounts (accountid, name) VALUES ('{Guid.NewGuid().ToString()}','Demen_Ins')"; // Insert コマンドを実行します。 int i = cmd.ExecuteNonQuery(); if (i != -1) { Console.WriteLine("Insert Operation Successful \n \n"); } } } |
SQLite RETURNING clause is used at the end of the INSERT statement to return the inserted values. More on the SQLite’s RETURNING clause can be found here. When providing the RETURNING clause, call the ExecuteReader method to obtain the inserted values. This method will return a reader which can be used to read the result table containing the newly inserted values.
C# |
コードのコピー
|
---|---|
static void ReturningClause() { using (var con = new C1D365SConnection(connstr)) { con.Open(); // Insert コマンドを作成します。 var cmd = con.CreateCommand(); cmd.CommandText = $"INSERT INTO accounts (accountid, name)" + $"VALUES ('{Guid.NewGuid().ToString()}','Demen_Ins') RETURNING accountid"; // Insert コマンドを実行し、返された値を持つリーダー オブジェクトを使用します。 var reader = cmd.ExecuteReader(); while(reader.Read()){ Console.WriteLine($"accountid value --> {reader["accountid"]}"); } } } |
This example retrieves data by executing the Select command.
C# |
コードのコピー
|
---|---|
static void ReadDataReader() { using (var con = new C1D365SConnection(connectionString)) { con.Open(); // Read コマンドを作成します。 var cmd = con.CreateCommand(); cmd.CommandText = "SELECT accountid, name FROM Accounts LIMIT 10"; // Readコマンドを実行し、取得したデータを表示します。 DbDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["accountid"], rdr["name"])); } Console.WriteLine("Read operation successful! \n\n"); } } |
This example modifies data by executing Update command.
C# |
コードのコピー
|
---|---|
static void UpdateDataReader() { using (var con = new C1D365SConnection(connectionString)) { con.Open(); // Update コマンドを作成します。 var cmd = con.CreateCommand(); cmd.CommandText = "UPDATE Accounts SET name = 'UPDNAME' WHERE name = 'Demen_Ins'"; // Update コマンドを実行します。 int i = cmd.ExecuteNonQuery(); if (i != -1) { Console.WriteLine("Update operation successful! \n\n"); } } } |
This example removes data by executing Delete command.
C# |
コードのコピー
|
---|---|
static void DeleteDataReader() { using (var con = new C1D365SConnection(connectionString)) { con.Open(); // Delete コマンドを作成します。 var cmd = con.CreateCommand(); cmd.CommandText = "DELETE FROM accounts WHERE name = 'Demen_Ins'"; // Delete コマンドを実行します。 int i = cmd.ExecuteNonQuery(); if (i != -1) { Console.WriteLine("Delete operation successful! \n\n"); } } } |
The C1D365SDataAdapter class can be used to retrieve a single result set containing all the data that matches a given query. The C1D365SDataAdapter uses its Fill method to fetch data from the data source. An empty DataTable instance is passed as an argument to the Fill method. Once the method returns, the DataTable instance is populated with the queried data. Since the Fill method must retrieve all the data from the data source before returning, the C1D365SDataAdapter is slower compared to the DbDataReader.
The following code examples demonstrate create, read, update, and delete operations from the data source using the C1D365SDataAdapter.
The example adds new records by executing the Insert command.
C# |
コードのコピー
|
---|---|
static void InsertDataAdapter() { using (var conn = new C1D365SConnection(connectionString)) { conn.Open(); // データテーブルにデータを入力します。 var adapter = new C1D365SDataAdapter(conn, "SELECT * FROM accounts LIMIT 10"); var dataTable = new DataTable(); adapter.Fill(dataTable); // Insert コマンドを作成します。 adapter.InsertCommand = new C1D365SCommand(conn); adapter.InsertCommand.CommandText = "INSERT INTO accounts (accountid, name) VALUES (@accountid,@name)"; adapter.InsertCommand.Parameters.Add(new C1DbParameter("@accountid", DbType.Guid, "accountid")); adapter.InsertCommand.Parameters.Add(new C1DbParameter("@name", DbType.String, "name")); adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; // 新しい行を挿入します。 DataRow AccountRow = dataTable.NewRow(); AccountRow["accountid"] = Guid.NewGuid(); AccountRow["name"] = "Rajesh"; dataTable.Rows.Add(AccountRow); // データベースを更新します。 adapter.Update(dataTable); } } |
This examples retrieves data by executing the Select command.
C# |
コードのコピー
|
---|---|
static void ReadDataAdapter() { using (var conn = new C1D365SConnection(connectionString)) { conn.Open(); var adapter = new C1D365SDataAdapter(conn, "SELECT accountid, name FROM Accounts WHERE name ='Updated_Name'"); var dataTable = new DataTable(); adapter.Fill(dataTable); } } |
This example modifies data by executing Update command.
C# |
コードのコピー
|
---|---|
static void UpdateDataAdapter() { using (var conn = new C1D365SConnection(connectionString)) { conn.Open(); // データテーブルにデータを入力します。 var adapter = new C1D365SDataAdapter(conn, "SELECT * FROM accounts"); var dataTable = new DataTable(); adapter.Fill(dataTable); // Update コマンドを作成します。 adapter.UpdateCommand = new C1D365SCommand(conn); adapter.UpdateCommand.CommandText = "UPDATE accounts SET emailaddress1=@EmailAddress1 WHERE name=@Name"; adapter.UpdateCommand.Parameters.Add(new C1DbParameter("@name", DbType.String, "name")); adapter.UpdateCommand.Parameters.Add(new C1DbParameter("@EmailAddress1", DbType.String, "emailaddress1")); adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; // 既存の行を更新します。 DataRow AccountRow = dataTable.Rows[0]; AccountRow["name"] = "Rajesh"; AccountRow["emailaddress1"] = "update@email.com"; // データベースを更新します。 adapter.Update(dataTable); } } |
This example removes data by executing Delete command.
C# |
コードのコピー
|
---|---|
static void DeleteDataAdapter() { using (var conn = new C1D365SConnection(connectionString)) { conn.Open(); // データテーブルにデータを入力します。 var adapter = new C1D365SDataAdapter(conn, "SELECT * FROM accounts LIMIT 10"); var dataTable = new DataTable(); adapter.Fill(dataTable); // Create Delete command adapter.DeleteCommand = new C1D365SCommand(conn); adapter.DeleteCommand.CommandText = "DELETE FROM accounts WHERE name = @name"; adapter.DeleteCommand.Parameters.Add(new C1DbParameter("@name", DbType.String, "name")); adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None; // Delete a row DataRow AccountRow = dataTable.Rows[3]; AccountRow["name"] = "Rajesh"; AccountRow.Delete(); // データベースを更新します。 adapter.Update(dataTable); } } |