PowerShell Excel imports SQL Server to update multiple table imports, which was slightly altered before.
CLS$SqlConnection= New-object System.Data.SqlClient.SqlConnection#To SqlConnection a pair of elephants$SqlConnection. ConnectionString ="server=192.168.1.1;database=sa;user=sa;pwd=123" #The connection string indicating the SqlConnection image$SQLCMD= New-object System.Data.SqlClient.SqlCommand#To SqlCommand a pair of elephants$SQLCMD. Connection =$SqlConnection$excelApp= New-object-COM Excel.Application$file=(dir D:\express.xlsx). FullName$book=$excelApp. Workbooks.Open ($file)$sheet=$book. Worksheets.item (1) $SQLCMD. commandtext="INSERT into [AppData]. [dbo]. [Expresscheckheader] ([Checkid], [UpdateName], [UpdateTime], [Headerstatus], [Expressco Mpany]) VALUES (' $ ($sheet. Cells.item (2,1). Value2) ', ' $ ($sheet. Cells.item (2,2). Value2) ', ' $ ($sheet. Cells.item (2,3). Value2) ', 0, ' $ ($sheet. Cells.item (2,4). Value2) ')"try{$SqlConnection. Open (); $intRezult=$SQLCMD. ExecuteNonQuery (); $SqlConnection. Close (); $sheet. Cells.item (2,9). Value2=' table header imported successfully! ‘; "------------------------------------------------------" "table header Import Successful! " "------------------------------------------------------"} catch {"table header save failed!"; "------------------------------------------------------" return; } $row= 5$count=1 while($true){ if(!$sheet. Cells.item ($row, 1). Value2) { Break; } $SQLCMD. commandtext="INSERT into [AppData]. [dbo]. [Expresscheckline] ([Checkid], [senddate], [Expressid], [sendtoaddress], [expcontent], [EXP Weight], [Expmoney], [Customer], [Expstatus]) VALUES (' $ ($sheet. Cells.item ($row, 1). Value2) ', ' $ ($sheet. Cells.item ($row, 2). Value2) ', ' $ ($sheet. Cells.item ($row, 3). Value2) ', ' $ ($sheet. Cells.item ($row, 4). Value2) ', ' $ ($sheet. Cells.item ($row, 5). Value2) ', $ ($sheet. Cells.item ($row, 6). Value2), $ ($sheet. Cells.item ($row, 7). Value2), ' $ ($sheet. Cells.item ($row, 8). Value2) ', 0)"try{$SqlConnection. Open (); $intRezult=$SQLCMD. ExecuteNonQuery (); $SqlConnection. Close (); $sheet. Cells.item ($row, 9). Value2=' Import succeeded! ‘; $count. toString () + ' bar saved successfully, barcode number: ' +$ ($sheet. Cells.item ($row, 3). Value2); } catch {$count. toString () + ' save failed, barcode number: ' +$ ($sheet. Cells.item ($row, 3). Value2); } $count++$row++}"------------------------------------------------------""The import operation is complete! Please check the import Excel file! ""------------------------------------------------------"$book. Save ()$book. Close ()$excelApp. Quit ()$book=$null$sheet=$null$excelApp=$null[GC]:: Collect ()
PowerShell Excel Import SQL Server