Sample .NET Framework Data Provider

The preceding topics include links to additional topics that contain sample code you can use to create a sample .NET Framework data provider. You can copy this code into blank text files, place the files in a directory, and compile them into a library per the instructions found in the section "Compiling the Sample .NET Framework Data Provider" later in this topic.

The following table shows the list of sample code files that are available for creating a sample .NET Framework data provider.

File name Description
TemplateConnection.vb

TemplateConnection.cs

Contains sample code for creating a Connection object. For more information, see Implementing a Connection.
TemplateTransaction.vb

TemplateTransaction.cs

Contains sample code for creating a Transaction object. For more information, see Implementing a Connection.
TemplateCommand.vb

TemplateCommand.cs

Contains sample code for creating a Command object. For more information, see Implementing a Command.
TemplateParameter.vb

TemplateParameter.cs

Contains sample code for creating a Parameter object that is a member of the Command.Parameters collection. For more information, see Implementing a Command.
TemplateParameterCollection.vb

TemplateParameterCollection.cs

Contains sample code for creating a ParameterCollection object to be used as the Command.Parameters property. For more information, see Implementing a Command.
TemplateDataReader.vb

TemplateDataReader.cs

Contains sample code for creating a DataReader object. For more information, see Implementing a DataReader.
TemplateDataAdapter.vb

TemplateDataAdapter.cs

Contains sample code for creating a DataAdapter object. For more information, see Implementing a DataAdapter.
SampleDb.vb

SampleDb.cs

Contains sample code that simulates a query and updates to a data source. For more information, see the "SampleDb" section later in this topic.

Compiling the Sample .NET Framework Data Provider

After you create the files from the sample code provided and place them in a folder, issue the following command to create the sample .NET Framework data provider as DotNetDataProviderTemplate.dll. DotNetDataProviderTemplate.dll can then be passed as a reference to a program that makes use of the DotNetDataProviderTemplate namespace.

To compile the Visual Basic version of the sample .NET Framework data provider, you can use the following command.

vbc /t:library /out:DotNetDataProviderTemplate.dll *.vb /r:system.dll /r:system.data.dll

To compile the C# version of the sample .NET Framework data provider, you can use the following command.

csc /t:library /out:DotNetDataProviderTemplate.dll *.cs /r:system.dll /r:system.data.dll

Using the Sample .NET Framework Data Provider

After you have compiled the sample .NET Framework data provider into DotNetDataProviderTemplate.dll, you can write a program to use the DotNetDataProviderTemplate namespace.

The following code example contains a program that creates instances of the sample DataReader and DataAdapter.

Imports System
Imports System.Data
Imports DotNetDataProviderTemplate
Imports Microsoft.VisualBasic

Class Sample
  Public Shared Sub Main()
    testReader()
    testAdapter()
  End Sub

  Public Shared Sub testReader()
    Dim conn As TemplateConnection = New TemplateConnection()
    conn.Open()
      
    Dim cmd As TemplateCommand = New TemplateCommand("select * from customers", conn)

    Dim reader As TemplateDataReader = cmd.ExecuteReader()

    Do while (reader.Read())
      Console.Write(reader.GetInt32(0) & vbTab)
      Console.Write(reader.GetString(1) & vbTab)
      Console.Write(reader.GetInt32(2))
      Console.WriteLine()
    Loop
    reader.Close()
      
    conn.Close()
  End Sub

  Public Shared Sub testAdapter()
    Dim conn As TemplateConnection = New TemplateConnection()
    Dim adapter As TemplateDataAdapter = New TemplateDataAdapter()

    adapter.SelectCommand = New TemplateCommand("select * from customers", conn)

    adapter.UpdateCommand = New TemplateCommand("update name, orderid values(@name, @orderid) where id = @id", conn)

    Dim parm As TemplateParameter = New TemplateParameter("@name", DbType.String, "name")
    adapter.UpdateCommand.Parameters.Add(parm)

    parm = New TemplateParameter("@orderid", DbType.Int32, "orderid")
    adapter.UpdateCommand.Parameters.Add(parm)

    parm = New TemplateParameter("@id", DbType.Int32, "id")
    adapter.UpdateCommand.Parameters.Add(parm)

    Dim ds As DataSet = New DataSet
    adapter.Fill(ds, "Customers")

    Console.WriteLine("------------------------")
    Console.WriteLine("DataSet contents after Fill:")
    printDataSet(ds)

    Console.WriteLine("------------------------")
    ds.Tables("Customers").Rows(2)("orderid") = 4199
    adapter.Update(ds, "Customers")

    Console.WriteLine("------------------------")
    Console.WriteLine("DataSet contents after Update:")
    printDataSet(ds)
    Console.WriteLine("------------------------")
  End Sub

  Public Shared Sub printDataSet(ds As DataSet)
    Dim table As DataTable
    Dim col As DataColumn
    Dim row As DataRow
    Dim i As Integer

    For Each table in ds.Tables
      For Each col in table.Columns
        Console.Write(col.ColumnName & vbTab & vbTab)
      Next
      Console.WriteLine()

      For Each row in table.Rows  
        For i = 0 To table.Columns.Count - 1  
          Console.Write(row(i).ToString() & vbTab & vbTab)
        Next
        Console.WriteLine()
      Next
    Next
  End Sub
End Class
[C#]using System;
using System.Data;
using DotNetDataProviderTemplate;

class Sample
{
  static void Main()
  {
    testReader();
    testAdapter();
  }

  static void testReader()
  {
    TemplateConnection conn = new TemplateConnection();
    conn.Open();
      
    TemplateCommand cmd = new TemplateCommand("select * from customers", conn);

    IDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
      Console.Write(reader.GetInt32(0) + "\t");
      Console.Write(reader.GetString(1) + "\t");
      Console.Write(reader.GetInt32(2));
      Console.WriteLine();
    }
    reader.Close();
    
    conn.Close();
  }

  static void testAdapter()
  {
    TemplateConnection  conn = new TemplateConnection();
    TemplateDataAdapter  adapter = new TemplateDataAdapter();

    adapter.SelectCommand = new TemplateCommand("select * from customers", conn);

    adapter.UpdateCommand = new TemplateCommand("update name, orderid values(@name, @orderid) where id = @id", conn);
    adapter.UpdateCommand.Parameters.Add("@name", DbType.String);
    adapter.UpdateCommand.Parameters.Add("@orderid", DbType.Int32);
    adapter.UpdateCommand.Parameters.Add("@id", DbType.Int32);

    DataSet ds = new DataSet();
    adapter.Fill(ds, "Customers");

    Console.WriteLine("------------------------");
    Console.WriteLine("Results of adapter.Fill");
    printDataSet(ds);
    Console.WriteLine("------------------------");

    ds.Tables["Customers"].Rows[2]["orderid"] = 4199;
    adapter.Update(ds, "Customers");

    Console.WriteLine("------------------------");
    Console.WriteLine("Results of adapter.Update");
    printDataSet(ds);
    Console.WriteLine("------------------------");
  }

  static void printDataSet(DataSet ds)
  {
    foreach (DataTable table in ds.Tables)
    {
      foreach (DataColumn col in table.Columns)
        Console.Write(col.ColumnName + "\t\t");
      Console.WriteLine();
      foreach (DataRow row in table.Rows)
      {
        for (int i = 0; i < table.Columns.Count; i++)
          Console.Write(row[i] + "\t\t");
        Console.WriteLine("");
      }
    }
  }
}

To compile the program in the preceding code example, ensure that a reference to DotNetDataProvider.dll is included.

The following example command will compile the program if it is stored in a file called "SampleTest.vb" it with the following command:.

vbc SampleTest.vb /r:system.dll /r:system.data.dll /r:system.xml.dll /r:DotNetDataProviderTemplate.dll

The following example command will compile the program if it is stored in a file called SampleTest.cs.

csc SampleTest.cs /r:system.dll /r:system.data.dll /r:system.xml.dll /r:DotNetDataProviderTemplate.dll

SampleDb

Along with the sample .NET Framework data provider implementation, a sample database is provided in this section to simulate SELECT and UPDATE operations to a data source. This sample database is named SampleDb, and is included with the .NET Framework data provider files. You will need to include the SampleDb.vb (for Visual Basic) or SampleDb.cs (for C#) files with your template files when compiling the sample .NET Framework data provider.

See Also

Implementing a .NET Framework Data Provider