A
- AccessControlEntry.ConditionToIdentities
- Access.Database
- ActiveDirectory.Domains
- AdobeAnalytics.Cubes
- AdoDotNet.DataSource
- AdoDotNet.Query
- AnalysisServices.Database
- AnalysisServices.Databases
- AzureStorage.BlobContents
- AzureStorage.Blobs
- AzureStorage.DataLake
- AzureStorage.DataLakeContents
- AzureStorage.Tables
B
- BinaryFormat.Binary
- BinaryFormat.Byte
- BinaryFormat.ByteOrder
- BinaryFormat.Choice
- BinaryFormat.Decimal
- BinaryFormat.Double
- BinaryFormat.Group
- BinaryFormat.Length
- BinaryFormat.List
- BinaryFormat.Null
- BinaryFormat.Record
- BinaryFormat.SignedInteger16
- BinaryFormat.SignedInteger32
- BinaryFormat.SignedInteger64
- BinaryFormat.Single
- BinaryFormat.Text
- BinaryFormat.Transform
- BinaryFormat.UnsignedInteger16
- BinaryFormat.UnsignedInteger32
- BinaryFormat.UnsignedInteger64
- BinaryFormat.7BitEncodedSignedInteger
- BinaryFormat.7BitEncodedUnsignedInteger
- Binary data
- Binary.ApproximateLength
- Binary.Buffer
- Binary.Combine
- Binary.Compress
- Binary.Decompress
- Binary.From
- Binary.FromList
- Binary.FromText
- Binary.InferContentType
- Binary.Length
- Binary.Range
- Binary.Split
- Binary.ToList
- Binary.ToText
- Binary.View
- Binary.ViewError
- Binary.ViewFunction
- Byte.From
C
- Cdm.Contents
- Character.FromNumber
- Character.ToNumber
- Combiner.CombineTextByDelimiter
- Combiner.CombineTextByEachDelimiter
- Combiner.CombineTextByLengths
- Combiner.CombineTextByPositions
- Combiner.CombineTextByRanges
- Comparer.FromCulture
- Comparer.Ordinal
- Comparer.OrdinalIgnoreCase
- Controlling byte order
- Csv.Document
- Cube.AddAndExpandDimensionColumn
- Cube.AddMeasureColumn
- Cube.ApplyParameter
- Cube.AttributeMemberId
- Cube.AttributeMemberProperty
- Cube.CollapseAndRemoveColumns
- Cube.Dimensions
- Cube.DisplayFolders
- Cube.MeasureProperties
- Cube.MeasureProperty
- Cube.Measures
- Cube.Parameters
- Cube.Properties
- Cube.PropertyKey
- Cube.ReplaceDimensions
- Cube.Transform
- Currency.From
D
- DateTime.AddZone
- DateTime.Date
- DateTime.FixedLocalNow
- DateTime.From
- DateTime.FromFileTime
- DateTime.FromText
- DateTime.IsInCurrentHour
- DateTime.IsInCurrentMinute
- DateTime.IsInCurrentSecond
- DateTime.IsInNextHour
- DateTime.IsInNextMinute
- DateTime.IsInNextNHours
- DateTime.IsInNextNMinutes
- DateTime.IsInNextNSeconds
- DateTime.IsInNextSecond
- DateTime.IsInPreviousHour
- DateTime.IsInPreviousMinute
- DateTime.IsInPreviousNHours
- DateTime.IsInPreviousNMinutes
- DateTime.IsInPreviousNSeconds
- DateTime.IsInPreviousSecond
- DateTime.LocalNow
- DateTime.Time
- DateTime.ToRecord
- DateTime.ToText
- Date.AddDays
- Date.AddMonths
- Date.AddQuarters
- Date.AddWeeks
- Date.AddYears
- Date.Day
- Date.DayOfWeek
- Date.DayOfWeekName
- Date.DayOfYear
- Date.DaysInMonth
- Date.EndOfDay
- Date.EndOfMonth
- Date.EndOfQuarter
- Date.EndOfWeek
- Date.EndOfYear
- Date.From
- Date.FromText
- Date.IsInCurrentDay
- Date.IsInCurrentMonth
- Date.IsInCurrentQuarter
- Date.IsInCurrentWeek
- Date.IsInCurrentYear
- Date.IsInNextDay
- Date.IsInNextMonth
- Date.IsInNextNDays
- Date.IsInNextNMonths
- Date.IsInNextNQuarters
- Date.IsInNextNWeeks
- Date.IsInNextNYears
- Date.IsInNextQuarter
- Date.IsInNextWeek
- Date.IsInNextYear
- Date.IsInPreviousDay
- Date.IsInPreviousMonth
- Date.IsInPreviousNDays
- Date.IsInPreviousNMonths
- Date.IsInPreviousNQuarters
- Date.IsInPreviousNWeeks
- Date.IsInPreviousNYears
- Date.IsInPreviousQuarter
- Date.IsInPreviousWeek
- Date.IsInPreviousYear
- Date.IsInYearToDate
- Date.IsLeapYear
- Date.Month
- Date.MonthName
- Date.QuarterOfYear
- Date.StartOfDay
- Date.StartOfMonth
- Date.StartOfQuarter
- Date.StartOfWeek
- Date.StartOfYear
- Date.ToRecord
- Date.ToText
- Date.WeekOfMonth
- Date.WeekOfYear
- Date.Year
- DB2.Database
- Decimal.From
- Diagnostics.ActivityId
- Diagnostics.Trace
- DirectQueryCapabilities.From
- Double.From
- Duration.Days
- Duration.From
- Duration.FromText
- Duration.Hours
- Duration.Minutes
- Duration.Seconds
- Duration.ToRecord
- Duration.TotalDays
- Duration.TotalHours
- Duration.TotalMinutes
- Duration.TotalSeconds
- Duration.ToText
E
F
G
H
I
L
- Lines.FromBinary
- Lines.FromText
- Lines.ToBinary
- Lines.ToText
- List.Accumulate
- List.AllTrue
- List.Alternate
- List.AnyTrue
- List.Average
- List.Buffer
- List.Combine
- List.ConformToPageReader
- List.Contains
- List.ContainsAll
- List.ContainsAny
- List.Count
- List.Covariance
- List.Dates
- List.DateTimes
- List.DateTimeZones
- List.Difference
- List.Distinct
- List.Durations
- List.FindText
- List.First
- List.FirstN
- List.Generate
- List.InsertRange
- List.Intersect
- List.IsDistinct
- List.IsEmpty
- List.Last
- List.LastN
- List.MatchesAll
- List.MatchesAny
- List.Max
- List.MaxN
- List.Median
- List.Min
- List.MinN
- List.Mode
- List.Modes
- List.NonNullCount
- List.Numbers
- List.Percentile
- List.PositionOf
- List.PositionOfAny
- List.Positions
- List.Product
- List.Random
- List.Range
- List.RemoveFirstN
- List.RemoveItems
- List.RemoveLastN
- List.RemoveMatchingItems
- List.RemoveNulls
- List.RemoveRange
- List.Repeat
- List.ReplaceMatchingItems
- List.ReplaceRange
- List.ReplaceValue
- List.Reverse
- List.Select
- List.Single
- List.SingleOrDefault
- List.Skip
- List.Sort
- List.Split
- List.StandardDeviation
- List.Sum
- List.Times
- List.Transform
- List.TransformMany
- List.Union
- List.Zip
- Logical.From
- Logical.FromText
The AdoDotNet.Query function is one of the many M functions available in Power Query. This function allows users to execute SQL queries against a variety of database systems, including Microsoft SQL Server, Oracle, and MySQL. In this article, we will explore the M code behind the AdoDotNet.Query function and how it can be used to access and manipulate data in a database.
Understanding AdoDotNet.Query
Before delving into the M code behind AdoDotNet.Query, it is helpful to understand the function itself. AdoDotNet.Query is a function that takes three arguments: connectionString, sqlStatement, and options. The connectionString argument is a string that defines the connection information for the database. The sqlStatement argument is a string that contains the SQL query to be executed. The options argument is an optional record that contains additional parameters for the query, such as the timeout value or the type of command to be executed.
When executed, the AdoDotNet.Query function returns a table that contains the results of the SQL query. This table can then be transformed and loaded into a variety of destinations using other Power Query functions.
The M Code Behind AdoDotNet.Query
The M code behind the AdoDotNet.Query function is relatively straightforward. At its core, the function is simply a wrapper around the .NET System.Data.SqlClient.SqlCommand class, which is used to execute SQL queries against a Microsoft SQL Server database.
Here is the M code for the AdoDotNet.Query function:
let AdoDotNet.Query = (connectionString as text, sqlStatement as text, optional options as record) =>
let
conn = AdoDotNetProvider.GetConnection(connectionString),
command = conn.CreateCommand(),
commandTimeout = if options <> null and options[CommandTimeout] <> null then options[CommandTimeout] else null,
commandType = if options <> null and options[CommandType] <> null then options[CommandType] else Sql.CommandType.Text,
resultTable = Table.FromRows(Sql.DataReader(command.ExecuteReader(CommandBehavior.CloseConnection), commandTimeout), command.ExecuteReader().GetSchemaTable())
in
resultTable
Let’s break down this code line by line:
– The first line defines the name of the function and its three arguments: connectionString, sqlStatement, and options.
– The second line creates a connection object using the AdoDotNetProvider.GetConnection function, which is a built-in Power Query function that returns a .NET System.Data.SqlClient.SqlConnection object.
– The third line creates a command object using the CreateCommand method of the connection object.
– The fourth and fifth lines check the options parameter to see if a command timeout or command type has been specified. If so, these values are used in the command object.
– The sixth line executes the SQL query using the ExecuteReader method of the command object and returns the results as a table using the Table.FromRows function.
Using AdoDotNet.Query in Power Query
Now that we understand the M code behind the AdoDotNet.Query function, let’s look at how it can be used in Power Query.
To use AdoDotNet.Query, we first need to create a new query in Power Query. We can do this by clicking the “New Source” button on the Home tab of the Power Query ribbon and selecting “Blank Query”.
Next, we need to enter the M code for the AdoDotNet.Query function. We can do this by clicking the “Advanced Editor” button on the View tab of the Power Query ribbon and pasting the following code:
let
connectionString = "Server=localhost;Database=MyDatabase;Trusted_Connection=True;",
query = "SELECT FROM MyTable",
results = AdoDotNet.Query(connectionString, query)
in
results
In this code, we define the connection string and SQL query to be executed, and then we call the AdoDotNet.Query function with these values. The results of the query are then returned as a table.
We can then use other Power Query functions to transform and load this data into a variety of destinations. For example, we could use the Table.SelectColumns function to select specific columns from the query results, or the Table.TransformColumnTypes function to change the data types of specific columns.
In this article, we have explored the M code behind the AdoDotNet.Query function in Microsoft Power Query. We have learned that this function is simply a wrapper around the .NET SqlCommand class and allows users to execute SQL queries against a variety of database systems. We have also seen how this function can be used in Power Query to access and manipulate data in a database. By understanding the M code behind this function, users can create custom data transformations and queries that meet their specific needs.