簡體   English   中英

如何在VS2012數據庫項目下的腳本中獲取輸入文件的引用

[英]How to get reference of input files in scripts under VS2012 Database project

我們有一個解決方案,其中添加了一個數據庫項目。 該項目創建數據庫表並在發布時插入數據。 我添加了一個部署后腳本,在其中調用了Postcode.sql腳本,該腳本將郵政編碼插入到我們的一個表中。 一切正常,除了當我想將CSV文件指定為批量插入的輸入時,無論我做什么,我總是得到“系統找不到指定的路徑”。 例外。 文件夾結構如下所示:

[DatabaseProject]
[InputFiles] //folder
- Postcodes.csv
[PostDeploymentScripts] //folder
- Postcode.PostDeployment.sql //this will call the Postcode.sql
- Postcode.sql

這是Postcode.sql中的(當前測試中)sql腳本:

DECLARE @Count int;
SELECT @Count = Count(*) FROM Postcode;
IF(@Count = 0)
BEGIN
BULK INSERT Postcode
FROM '.\InputFiles\Postcodes.csv'
WITH
(
ROWTERMINATOR = '\n'
)
END
GO

我將其從更改為“ FROM'c:\\ TFS \\ Project \\ InputFiles \\ Postcodes.csv'”以使其正常運行,但是我在這里需要一個路徑,該路徑也可以在其他開發人員的計算機上使用,因為我確定他們使用用於存儲解決方案的不同文件夾。

所以問題是:在SQL腳本文件中指向該Postcodes.csv的正確格式(如果有)是什么?

好吧,我決定不使用sql腳本文件,而是立即從這樣的部署腳本中運行命令(我使插入腳本變得更好,更短):

-- Insert Postcodes
IF(NOT EXISTS(SELECT * FROM Postcode)) BEGIN
    BULK INSERT Postcode
    FROM [$(PostcodesFilePath)]
    WITH (ROWTERMINATOR = '\n')
END

為了能夠運行此程序,我必須指定一個名為PostcodesFilePath的變量。 去做這個:

1. Open properties of the project.
2. Go to SQLCMD variable tab.
3. Add $(PostcodesFilePath) to the list of variable and set ONLY the default
   value column to $(ProjectDir)InputFiles\Postcodes.csv
4. To make sure the value is good, just save your project, close properties
   and reopen it. The default column will contain the full path to your csv.
5. If you would like to run the post deployment script, just publish your
   database project. In case you have build errors it is possible that you need
   to click on 'SQLCMD mode' button in your post deployment editor.

暫無
暫無

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

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