我有有几个数据列的CSV文件。
CSV文件看起来像
字段1:测试1
场2:Test2的
字段3:Test3的,TEST4,TEST5
在这种情况下该库可以为我场终止使用我的意思是,如果我用这个查询插入CSV文件导入shopifyitem
表作为你假设数据域插入无法正常
BULK INSERT shopifyitem
FROM 'c:\test.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
所以,我可以使用哪些字段终止 ?
非常感谢你提前....
我不认为你将能够导入的格式不经过一定的预处理。 亚伦暗示,这不是一个标准的CSV。
如果你不能得到的文件重新格式化,有几种方式来获取数据到一个导入格式:
http://www.timmitchell.net/post/2013/01/14/ragged-flat-file-processing-in-ssis/
http://www.rad.pasfu.com/index.php?/archives/38-Script-Component-as-Source-SSIS.html
http://msdn.microsoft.com/en-us/library/ms136060.aspx (向下滚动到“平面文件源实施例”)
这些都是SSIS的解决方案,充分利用.NET的大部分工作。 我更喜欢仅仅是因为内置的生产力工具SSIS。 它可以与像一个控制台应用程序或PowerShell脚本任何文字处理器来完成(如果你真的有你的手一段时间)。
我喜欢用一个流阅读器的脚本组件源,但蒂姆·米切尔有一个有趣的选择。
这是导入文本或CSV文件导入数据库的代码:
String SQL = "BULK INSERT [staging_db].[dbo].[TEST] FROM 'D:\\test.txt' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')";
using System;
using System.Data;
using System.Data.SqlClient;
namespace SqlBulkInsertExample
{
class Program
{
static void Main(string[] args)
{
DataTable prodSalesData = new DataTable("ProductSalesData");
// Create Column 1: SaleDate
DataColumn dateColumn = new DataColumn();
dateColumn.DataType = Type.GetType("System.DateTime");
dateColumn.ColumnName = "SaleDate";
// Create Column 2: ProductName
DataColumn productNameColumn = new DataColumn();
productNameColumn.ColumnName = "ProductName";
// Create Column 3: TotalSales
DataColumn totalSalesColumn = new DataColumn();
totalSalesColumn.DataType = Type.GetType("System.Int32");
totalSalesColumn.ColumnName = "TotalSales";
// Add the columns to the ProductSalesData DataTable
prodSalesData.Columns.Add(dateColumn);
prodSalesData.Columns.Add(productNameColumn);
prodSalesData.Columns.Add(totalSalesColumn);
// Let's populate the datatable with our stats.
// You can add as many rows as you want here!
// Create a new row
DataRow dailyProductSalesRow = prodSalesData.NewRow();
dailyProductSalesRow["SaleDate"] = DateTime.Now.Date;
dailyProductSalesRow["ProductName"] = "Nike";
dailyProductSalesRow["TotalSales"] = 10;
// Add the row to the ProductSalesData DataTable
prodSalesData.Rows.Add(dailyProductSalesRow);
// Copy the DataTable to SQL Server using SqlBulkCopy
using (SqlConnection dbConnection = new SqlConnection("Data Source=ProductHost;Initial Catalog=dbProduct;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;"))
{
dbConnection.Open();
using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
{
s.DestinationTableName = prodSalesData.TableName;
foreach (var column in prodSalesData.Columns)
s.ColumnMappings.Add(column.ToString(), column.ToString());
s.WriteToServer(prodSalesData);
}
}
}
}
}
尝试使用:
ROWTERMINATOR = '0x0a'