Files
sqltoolsservice/docs/guide/using_the_dotnet_api.md
Karl Burtram 4184eae8a1 Update docs (#200)
This is a documentation-only update so automerging.  Please review the commit if there are any follow-ups requested.

* Update .gitignore for docfx genertated files

* Update documenation (part 1)

* Update docs and add some samples

* More doc updates
2016-12-20 15:52:46 -08:00

2.6 KiB

Using the SQL Tools Service .NET API

NOTE: The API Reference is the best starting point for working directly with the .NET API.

An example of using the JSON RPC API from a .Net Core console application is available at docs/samples/jsonrpc/netcore.
The following snippet provides a basic example of how to connect to a database and execute a query.

internal static async Task ExecuteQuery(string query)
{    
    // create a temporary "workspace" file
    using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
    // create the client helper which wraps the client driver objects
    using (ClientHelper testHelper = new ClientHelper())
    {
        // connnection details
        ConnectParams connectParams = new ConnectParams();
        connectParams.Connection = new ConnectionDetails();
        connectParams.Connection.ServerName = "localhost";
        connectParams.Connection.DatabaseName = "master";
        connectParams.Connection.AuthenticationType = "Integrated";

        // connect to the database
        await testHelper.Connect(queryTempFile.FilePath, connectParams);

        // execute the query
        QueryExecuteCompleteParams queryComplete = 
            await testHelper.RunQuery(queryTempFile.FilePath, query);

        if (queryComplete.BatchSummaries != null && queryComplete.BatchSummaries.Length > 0)
        {
            var batch = queryComplete.BatchSummaries[0];
            if (batch.ResultSetSummaries != null && batch.ResultSetSummaries.Length > 0)
            {
                var resultSet = batch.ResultSetSummaries[0];

                // retrive the results
                QueryExecuteSubsetResult querySubset = await testHelper.ExecuteSubset(
                    queryTempFile.FilePath, batch.Id, 
                    resultSet.Id, 0, (int)resultSet.RowCount);

                // print the header
                foreach (var column in resultSet.ColumnInfo)
                {
                    Console.Write(column.ColumnName + ", ");
                }
                Console.Write(Environment.NewLine);

                // print the rows
                foreach (var row in querySubset.ResultSubset.Rows)
                {
                    for (int i = 0; i < resultSet.ColumnInfo.Length; ++i)
                    {
                        Console.Write(row.GetValue(i) + ", ");
                    }
                    Console.Write(Environment.NewLine);
                }
            }                    
        }

        // close database connection
        await testHelper.Disconnect(queryTempFile.FilePath);
    }
}