简体   繁体   English

计划导入 csv 到 SQL Server 2014 Express 版

[英]Schedule import csv to SQL Server 2014 Express edition

Is there a way to have a .csv or .txt imported into SQL Server automatically?有没有办法将.csv.txt自动导入 SQL Server?

I know how to do it manually using Date import & Export tool.我知道如何使用日期导入和导出工具手动执行此操作。 But is it possible to do it automatically?但是有可能自动完成吗?

You can use Windows Task Scheduler to run bcp commands automatically.您可以使用Windows 任务计划程序自动运行bcp命令。 The command that will be run automatically will import your csv file using bulk copy program (bcp) utility.将自动运行的命令将使用批量复制程序 (bcp) 实用程序导入您的 csv 文件。 It can import or export data from/to files.它可以从/到文件导入或导出数据。 For example to import a csv file to a table in SQL Server, you can use command like this:例如,要将 csv 文件导入 SQL Server 中的表,您可以使用如下命令:

bcp.exe dbo.MyTable in "C:\Some Folder\Data.csv" -s MYPC\SQLEXPRESS -d MyDatabase -U LoginName -P StrongP@ssw0rd

Where:在哪里:

  • dbo.MyTable is the schema and table name, where data should be imported. dbo.MyTable是模式和表名,数据应该被导入。
  • in tells the direction (put data in the database, or get data out of it). in指示方向(将数据放入数据库,或从中取出数据)。
  • "C:\\Some Folder\\Data.csv" is the name and path to the file holding the data to be imported. "C:\\Some Folder\\Data.csv"是保存要导入数据的文件的名称和路径。
  • MYPC\\SQLEXPRESS is the computer and SQL Server instance name. MYPC\\SQLEXPRESS是计算机和 SQL Server 实例名称。
  • MyDatabase is the name of the database, where dbo.MyTable is. MyDatabase是数据库的名称,其中dbo.MyTable是。
  • LoginName and StronP@ssw0rd are the credentials to be used to connect to the server (or -E instead of -U and -P to connect using Windows Authentication). LoginNameStronP@ssw0rd是用于连接到服务器的凭据(或-E而不是-U-P以使用 Windows 身份验证进行连接)。

Then create a new scheduled task ( Start -> Task Scheduler -> Create Basic Task ) and set a schedule according your requirements (eg daily at 3:00 AM) to run the command above.然后创建一个新的计划任务( Start -> Task Scheduler -> Create Basic Task )并根据您的要求设置计划(例如每天凌晨 3:00)以运行上述命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM