Pregunta

How can I connect to SQL Server from command prompt using Windows authentication?

This command

Sqlcmd -u username -p password 

assumes a username & password for the SQL Server already setup

Alternatively how can I setup a user account from command prompt?

I've SQL Server 2008 Express on a Windows Server 2008 machine, if that relates.

¿Fue útil?

Solución

You can use different syntax to achieve different things. If it is windows authentication you want, you could try this:

sqlcmd /S  /d  -E

If you want to use SQL Server authentication you could try this:

sqlcmd /S  /d -U -P 

Definitions:

/S = the servername/instance name. Example: Pete's Laptop/SQLSERV
/d = the database name. Example: Botlek1
-E = Windows authentication.
-U = SQL Server authentication/user. Example: Pete
-P = password that belongs to the user. Example: 1234

Hope this helps!

Otros consejos

Try This :

--Default Instance

SQLCMD -S SERVERNAME -E

--OR
--Named Instance

SQLCMD -S SERVERNAME\INSTANCENAME -E

--OR

SQLCMD -S SERVERNAME\INSTANCENAME,1919 -E

More details can be found here

This might help..!!!

 SQLCMD -S SERVERNAME -E 

type sqlplus/"as sysdba" in cmd for connection in cmd prompt

After some tries, these are the samples I am using in order to connect:

Specifying the username and the password:

sqlcmd -S 211.11.111.111 -U NotSA -P NotTheSaPassword

Specifying the DB as well:

sqlcmd -S 211.11.111.111 -d SomeSpecificDatabase -U NotSA -P NotTheSaPassword

here is the commend which is tested Sqlcmd -E -S "server name" -d "DB name" -i "SQL file path"

-E stand for windows trusted

If you are running Linux, Mac OS or Windows, you can use MSSQL-CLI. Read the tutorial at https://www.mssqltips.com/sqlservertip/5298/new-interactive-command-line-tool-mssqlcli-for-sql-server/

With either of 2 commands below, I could log in SQL Server 2019 Express through Windows authentication:

sqlcmd -S DESKTOP-5K4TURF\SQLEXPRESS -E
sqlcmd /S DESKTOP-5K4TURF\SQLEXPRESS /E

This way you can connect easily:

  1. sqlcmd -S Your Server Name -U 'Login username'-P 'password'

  2. Type command: select name from sys.database and will show >1 in next line means database server connected

  3. Type go and pres enter will list all database(s)

  4. You can select any particular one and can go ahead

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top