2
Good afternoon guys I’m having trouble making a connection with sql, whenever I try to make the connection the following error occurs:
('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')
File "C:\Users\ytalos\Documents\Ytalo\Projeto1\Conexão.py", line 15, in conectar_com_banco
conexao = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
File "C:\Users\ytalos\Documents\Ytalo\Projeto1\Conexão.py", line 18, in <module>
cursor=conectar_com_banco('DW')
I wanted to authenticate the database with Windows credentials.
Name of the Bank: DW
Server: X
def conectar_com_banco(usuario):
if usuario in 'DW':
server = 'X'
database = 'dw'
username = 'teste'
password = 'teste1'
else:
print('funcao_nao_encontrado')
import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cur=cnxn.cursor()
return(cur)
cursor=conectar_com_banco('DW')
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
print(row)
It would have to use windows user and password as authentication on connection?
– Nidorus
Yes, instead of user and password you can remove them and just add the parameter
Trusted_Connection=yes;
. I edited my answer to contemplate your doubt.– Igor Cavalcanti
Thank you so much for your help Igor :)
– Nidorus