DatabaseProvider.ExecuteQuery Method
IIS 7.0
Returns an array of query results after executing a database query.
Assembly: Microsoft.Web.Management.DatabaseManager (in Microsoft.Web.Management.DatabaseManager.dll)
public abstract QueryResult[] ExecuteQuery( string connectionString, Query query )
Parameters
- connectionString
- Type: System.String
The connection string for the database connection.
- query
- Type: Microsoft.Web.Management.DatabaseManager.Query
The Query object that contains the query statement to execute.
Return Value
Type: Microsoft.Web.Management.DatabaseManager.QueryResult[]An array of QueryResult objects.
The following code sample illustrates an example ExecuteQuery method that returns an array of query results from a database query.
public override QueryResult[] ExecuteQuery(
string connectionString,
Query query )
{
List<QueryResult> results = new List<QueryResult>();
QueryResult result = new QueryResult();
try
{
// Create a new OLEDB connection using the connection string.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query.Statement, connection);
// Open the database connection.
connection.Open();
// Execute the query and access the data.
OleDbDataReader reader = command.ExecuteReader();
// Add the results to the query list.
results.Add(GetQueryResult(reader));
// Close the database connection.
reader.Close();
}
// Return the query results.
return results.ToArray();
}
catch(Exception ex)
{
throw new ProviderException(ex.Message);
}
}
...
private QueryResult GetQueryResult(OleDbDataReader reader)
{
QueryResult result = new QueryResult();
int fieldCount = reader.FieldCount;
for (int i = 0; i < fieldCount; i++)
{
QueryColumnMetadata metadata = new QueryColumnMetadata();
metadata.Name = reader.GetName(i);
result.ColumnMetadata.Add(metadata);
}
while (reader.Read())
{
object[] itemData = new object[fieldCount];
for (int i = 0; i < fieldCount; i++)
{
itemData[i] = ConvertToSerializable(reader[i]);
}
result.QueryResults.Add(itemData);
}
return result;
}
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.