SelectCommand uses its own Connection if it is created. Otherwise SelectCommand uses Connection property.
If the SelectCommand property is not set, DbDataTable generates necessary SQL statements automatically.
public void UseDataTable(DbDataTable myDataTable, DbCommand myCommand, DbConnection myConnection) { myDataTable.Connection = myConnection; myCommand.CommandText = "SELECT * FROM Test.Dept"; myDataTable.SelectCommand = myCommand; try { myDataTable.Active = true; foreach(DataRow myRow in myDataTable.Rows) { foreach(DataColumn myCol in myDataTable.Columns) { Console.Write(myRow[myCol]+"\t"); } Console.WriteLine(); } } finally { myDataTable.Active = false; } }
Public Sub UseDataTable(ByVal myDataTable As DbDataTable, ByVal myCommand As DbCommand, ByVal myConnection As DbConnection) myDataTable.Connection = myConnection myCommand.CommandText = "SELECT * FROM Test.Dept" myDataTable.SelectCommand = myCommand Try myDataTable.Active = True Dim myRow As DataRow Dim myCol As DataColumn For Each myRow In myDataTable.Rows For Each myCol In myDataTable.Columns Console.Write(myRow(myCol) & Chr(9)) Next myCol Console.WriteLine() Next myRow Finally myDataTable.Active = False End Try End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2