簡體   English   中英

通過Ado.NET執行SQL腳本,結果類似於SSMS

[英]Executing SQL scripts via Ado.NET with the results like in SSMS

我有一個批處理文件,其內容如下:

@Echo Off    
goto skipcomments
rem  
:skipcomments

call setup-CIpatch-db.bat
if not "%UserName%"=="" goto ok

echo You must edit setup-CIpatch-db.bat to set variables.  
echo This script no longer takes command line arguments.
goto end
:ok


if exist *.out  del *.out

echo CoreIssue FleetOne Maintenance Release
echo working... DO NOT CLOSE
REM ************************************************
REM Script in which Print Statement has been removed
REM ************************************************


REM ************************************************
REM Create Scripts for Tables
REM ************************************************


REM ************************************************
REM Alter Scripts for Tables
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i AlterTablecardBacktable07312012.sql -o AlterTablecardBacktable07312012.out





REM ************************************************
REM Data Updates
REM ************************************************

osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i AlterDrpandCrePrimKeyCardback.sql -o AlterDrpandCrePrimKeyCardback.out

REM ************************************************
REM Performance Updates
REM ************************************************




REM ************************************************
REM Security Update Scripts
REM ************************************************


REM ************************************************
REM Reports scripts
REM ************************************************



REM ************************************************
REM New or Altered Stored Procedures
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_RejectAccounts_qsel.sql -o sp_RejectAccounts_qsel.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_RejectAccounts_sel.sql -o sp_RejectAccounts_sel.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_GetCompanyRecordCardRequest_01Aug2012.sql -o sp_GetCompanyRecordCardRequest_01Aug2012.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i SPGetCreditTransactions.sql -o SPGetCreditTransactions.out




REM ************************************************
REM Grant.sql for new or altered SP
REM ************************************************


REM ************************************************
REM Platform TranID Change Scripts
REM ************************************************


REM ************************************************
REM Version number update
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i version.sql -o version.out


echo All Scripts have been started 
:end
pause

該腳本在同一文件夾中執行SQL文件,並生成擴展名為'.out'的文件。 這些只是結果。 同樣的,當我們在執行SSMS消息窗口的任何查詢,等的影響等1行中的批處理文件執行完所有腳本(通過ADO.net/C#)后,我們檢查了文件,如“錯誤”的關鍵字等等

現在,我已經獲得了使它自動化的任務,這就是我正在做的事情:
在臨批處理文件獲取.SQL文件的名稱和使用的SqlConnection執行它們,SqlCommand的等等的問題是,當查詢中有任何錯誤,則拋出異常。 我需要的是查詢應該執行任何操作,並且我應該能夠得到結果。 (如受影響的1行等)。 像在SSMS中一樣,當我們執行任何錯誤的查詢時,它都會執行並在“消息”窗格中顯示錯誤。

誰能指導我?

查看MSDN上的文檔oSql支持2個參數-m-r ,它們應該可以更好地處理錯誤輸出。

另外,作為批處理腳本的一部分,您可以檢查錯誤級別,例如:

注意:我使用標簽來創建一個函數來執行我的腳本,而無需重復所有錯誤代碼

REM run list of scripts
call :UpgradeDatabase AlterTablecardBacktable07312012.sql
call :UpgradeDatabase version.sql

goto :eof

:UpgradeDatabase

set upgradeScript=%~1

echo Running script %upgradeScript% on database %databaseName%...
sqlcmd -S %serverName% -d %databaseName% -U %userName% -P %password% -i %upgradeScript% -o %logPath%\%upgradeScript%.log
if %errorlevel% == 0 (
    echo Script %upgradeScript% on database %databaseName% successful
) ELSE (
    echo Script %upgradeScript% on database %databaseName% FAILED
    echo. > %logPath%\ERROR_%upgradeScript%.log
)
echo.
echo.

goto :eof

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM