Joel.Net.DatabaseUtility.dll Class Library

DatabaseUtility.ExecuteDataTable Method (String, CommandType, SqlParameter[])

Sends the System.Data.SqlClient.SqlCommand.CommandText to the System.Data.SqlClient.SqlCommand.Connection, and builds a System.Data.DataTable.

[Visual Basic]
Shared  OverloadsPublic Function ExecuteDataTable( _ 
   ByVal commandText As String, _ 
   ByVal commandType As CommandType, _ 
   ByVal ParamArray parameters As SqlParameter() _ 
) As DataTable
[C#]
public static DataTable ExecuteDataTable(
   string commandText,
   CommandType commandType,
   params SqlParameter[] parameters
);
[C++]
public: static DataTable* ExecuteDataTable(
   String* commandText,
   CommandType commandType,
   SqlParameter* parameters __gc[]
);
[JScript]
public static function ExecuteDataTable(
   String commandText,
   CommandType commandType,
   SqlParameter[] parameters
): DataTable;

Parameters

commandText
The text of the query.
commandType
Specifies how a command string is interpreted.
parameters
A list of type System.Data.SqlClient.SqlParameter that maps to the System.Data.SqlClient.SqlCommand.

Return Value

A representation of one table of in-memory data.

Remarks

Use the ExecuteDataTable method to retrieve a System.Data.DataTable from a database. This requires less code than using the SqlDataAdapter.Fill method, performing the operations necessary to generate the table of in-memory data returned by a SqlDataAdapter.

Example

[C#, Visual Basic] The following example creates a SqlDataAdapter and then executes it using the Fill method. The example is passed a string that is a Transact-SQL statement that returns an aggregate result.

[C#]
DatabaseUtility.Connection = new SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;");

DataTable customers = DatabaseUtility.ExecuteDataTable(
    "Northwind", 
    "SELECT * FROM Customers WHERE CustomerID = '@CustomerID'",
    CommandType.Text,
    new SqlParameter("@CustomerID", "ALFKI")
);
[Visual Basic]
DatabaseUtility.Connection = New SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;")

DataTable customers = DatabaseUtility.ExecuteDataTable( _
    "Northwind", _
    "SELECT * FROM Customers WHERE CustomerID = '@CustomerID'", _
    CommandType.Text, _
    new SqlParameter("@CustomerID", "ALFKI") _
)

See Also

DatabaseUtility Class | Joel.Net Namespace | DatabaseUtility.ExecuteDataTable Overload List