Domanda

I'm able to connect to SQL Server using SQL database name but my requirement is that I need to connect to sql server without connecting to database.

I've the following information available with me Port no., Instance name, db user/password, IP address.

My current command is this

engine_handle = create_engine('mssql+pyodbc://sa:pass@<IP address>/master', echo=False)

Now I'm able to connect because i've given db name - master but if i remove db name and give instance name or leave it altogether. i'll get the following error.

"engine = create_engine('mssql+pyodbc://sa:pass@IP address', echo=True)"

return self.dbapi.connect(*cargs, **cparams) DBAPIError: (Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') None None

It is ok If i can connect using instance name instead of DB name.

any help is appreciated.

È stato utile?

Soluzione

Use the URL that worked for you originally.

engine_handle = create_engine('mssql+pyodbc://sa:pass@<IP address>/master', echo=False)

Then you can issue CREATE DATABASE command, or whatever other operation you need to do.

When you leave out the database name, sqlalchemy is looking for a DSN named after the IP address you specify. Of course, the DSN doesn't exist and the exception is thrown. You must use a valid connection URL like those documented here.

Regarding the comment about not wanting to connect to a database - Each time you connect to SQL Server, your connection must have some database set for the connection. When created, your login had a default database assigned (if not specified, master is used).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top