Hello! Tell me, please, how to display the contents of the database table via the Windows command line with one command. You need to output without using sqlcmd.
2 answers
One command with sqlcmd :
sqlcmd -S hostName -d dbName -Q "select * from TableName" If sqlcmd cannot be used, another option is to use powershell . To do this, create a text file with the extension .ps1 approximately the following content:
$hostName = "hostName" $dbName = "dbName" $connectionString = "Server = $hostName; Database = $dbName; Integrated Security = True;" $connection = New-Object System.Data.SqlClient.SqlConnection $connection.ConnectionString = $connectionString $connection.Open() $cmd = New-Object System.Data.SqlClient.SqlCommand $cmd.CommandText = "SELECT * FROM TableName" $cmd.Connection = $connection $adapter = New-Object System.Data.SqlClient.SqlDataAdapter $adapter.SelectCommand = $cmd $dataTable = New-Object System.Data.DataTable $adapter.Fill($dataTable) > $null $connection.Close() foreach ($row in $dataTable.Rows) { for ($i=0; $i -lt $dataTable.Columns.Count; $i++) { $colName = $dataTable.Columns[$i]; $val = $row.ItemArray[$i]; if ([DBNull]::Value.Equals($val)) { write-host $colName " = NULL" } else { write-host $colName " = " $val } } write-host } which is then launched from the console with one command
powershell -File <ИмяФайлаСкрипта>.ps1 - oneFor powershell, there is also Invoke-Sqlcmd, but it requires SQL Server installed. - PashaPash ♦
- @ i-one, thanks for the reply! I was given a hint that I need to write a small program using gscript or vscript, which will display the contents of the table - Uchenitsa
|
The bcp program is used to copy data between SQL Server and a user-formatted data file. Using the bcp program, you can ... export data from tables to data files.
|
sqlcmd -S hostName -d dbName -Q "select * from TableName". However, this: " forbidden to use sqlcmd " is a rather strange requirement. How else? You can probably write a script for powershell. Or powershell is also prohibited to use? - i-one