I tried connecting to a MS SQL database using azureml.dataprep
in an Azure Notebook, as outlined in https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-load-data#load-sql-data, using MSSqlDataSource, using code of the form
import azureml.dataprep as dprep
secret = dprep.register_secret(value="[SECRET-PASSWORD]", id="[SECRET-ID]")
ds = dprep.MSSQLDataSource(server_name="[SERVER-NAME]",
database_name="[DATABASE-NAME], [PORT]",
user_name="[DATABASE-USERNAME]",
password=secret)
Setting [DATABASE-USERNAME]
equal to MYWINDOWSDOMAIN\\MYWINDOWSUSERNAME
and the password [SECRET-PASSWORD]
coinciding with my Windows password (i.e. trying to use Windows authentication).
After firing a query with
dataflow = dprep.read_sql(ds, "SELECT top 100 * FROM [dbo].[MYTABLE]")
dataflow.head(5)
I get
ExecutionError: Login failed.
I could connect to other databases without Windows Authentication fine. What am I doing wrong?
Consider using SQL server authentication as a workaround/alternative solution to connect to that db (the same dataflow
syntax will work):
import azureml.dataprep as dprep
secret = dprep.register_secret(value="[SECRET-PASSWORD]", id="[SECRET-ID]")
ds = dprep.MSSQLDataSource(server_name="[SERVER-NAME],[PORT]",
database_name="[DATABASE-NAME]",
user_name="[DATABASE-USERNAME]",
password=secret)
Note that the usage of dataprep
is deprecated, sqlalchemy
can be used an alternative
import pandas as pd
from sqlalchemy import create_engine
def mssql_engine(user = "[DATABASE-USERNAME]",
password = "[SECRET-PASSWORD]",
host = "[SERVER-NAME],[PORT]",
db = "[DATABASE-NAME]"):
engine = create_engine(f'mssql+pyodbc://{user}:{password}@{host}/{db}?driver=SQL+Server')
return engine
query = "SELECT ..."
df = pd.read_sql(query, mssql_engine())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With