我有喜歡的內容之一批處理文件:通過執行與結果Ado.NET SQL腳本像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文件相同的文件夾,並用」擴展名的文件生成。去」。這些只是包含結果;與在SSMS消息窗口中執行任何查詢時相同,如1行受影響等。在批處理文件完成執行所有腳本(通過ADO.net/C#)後,我們必須檢查out文件中的關鍵字,如「error」等
現在,我一直在給定的任務自動執行此,這裏是我在做什麼:
在臨批處理文件抓取.SQL文件的名稱和使用的SqlConnection,SqlCommand的等問題是執行它們,當查詢中有任何錯誤,則拋出異常。我需要的是查詢應該執行任何操作,我應該能夠得到結果。 (如1行受影響等)。就像在SSMS中一樣,當我們執行任何錯誤的查詢時,它會在消息窗格中執行並顯示錯誤。
任何人都可以引導我嗎?
這與C#和ADO.Net有什麼關係? –
如果腳本錯誤,您如何期望它完成?你是說如果第一次調用'osql'是錯誤的,你希望前面的腳本仍然執行第二次,第三次,第n次調用'osql'?或者你是否說如果script1.sql的一部分失敗,那麼該腳本的其餘部分必須繼續執行? –
仍然有點困惑 - 因此最初批處理文件正在使用C#執行(仍然不明白ADO.Net的鏈接),現在您可以自動執行錯誤部分,因此您不會長時間執行批處理文件,但會剝離sql腳本並在您的C#應用程序中逐一執行? –