Hi
I need to run the powershell command from mvc application. | want to read the data from the database table (having more than 2crore data) and create csv file and download the file to users download folder. Can someone please check and help me to resolve the issue?
$DBName = "testdb"
$FilePath = "C:\\test\\File1.csv"
$SQLQuery= @"
select * from testsummary
"@
Invoke-Sqlcmd_ -Serverlnstance dbinst -Database $DBName -Query $SQLQuery | Export-
CSV $FilePath -NoTypelnformation
Thanks,
Anu

Amit MohantyRank #112 · 20K reputationPosted Feb 14, 2023, 12:37 AM EST
Try this
#Variable to hold variable $SQLServer = "your_server_name" $SQLDBName = "database_name" $uid ="sa" $pwd = "password" $delimiter = ";" #SQL Query $SqlQuery = "SELECT * FROM $SQLDBName.dbo.MEQA5TableLogs;" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;" $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery $SqlCmd.Connection = $SqlConnection $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd #Creating Dataset $DataSet = New-Object System.Data.DataSet $SqlAdapter.Fill($DataSet) $DataSet.Tables[0] | Out-File "D:\export.csv" $DataSet.Tables[0] | export-csv -Delimiter $delimiter -Path "D:\export.csv" -NoTypeInformationSave the avobe script to a ps1 type file and run the file using powershell.
Anu200 reputationPosted Feb 14, 2023, 2:19 AM EST
Thanks,
Anu