将数据从文件复制到数据存储
问题描述:
是否可以导出以插入查询?将数据从文件复制到数据存储
我有一个疑问,在页面(http://www.filehelpers.net/)中的图像“高级用途的库:将数据从文件复制到数据存储”但我找不到信息,并知道我是否可以导出到SQL。
答
这应该工作:
[DelimitedRecord("|")]
public class CustomersVerticalBar
{
public string CustomerID;
public string CompanyName;
public string ContactName;
public string ContactTitle;
public string Address;
public string City;
public string Country;
}
class Program
{
private static string GetInsertSqlCust(object record)
{
CustomersVerticalBar obj = (CustomersVerticalBar)record;
return String.Format("INSERT INTO Customers (Address, City, CompanyName, ContactName, ContactTitle, Country, CustomerID) " +
" VALUES ('{0}' , '{1}' , '{2}' , '{3}' , '{4}' , '{5}' , '{6}' ); ",
obj.Address,
obj.City,
obj.CompanyName,
obj.ContactName,
obj.ContactTitle,
obj.Country,
obj.CustomerID
);
}
static void Main(string[] args)
{
SqlServerStorage storage = new SqlServerStorage(typeof(CustomersVerticalBar));
storage.ServerName = "MYSERVER";
storage.DatabaseName = "Northwind";
// Comment this for Windows Auth
storage.UserName = "shamp00";
storage.UserPass = "whatever";
storage.InsertSqlCallback = new InsertSqlHandler(GetInsertSqlCust);
storage.InsertRecords(CommonEngine.ReadFile(typeof(CustomersVerticalBar), "infile.txt"));
Console.ReadKey();
}
}
我敢肯定,曾经有一些这方面的文档 - 他们似乎从最新的网站缺少的。您可以查看FileHelpers源代码中的单元测试some more examples。
向我们展示您的尝试。您没有放入任何代码,并且添加了到第三方网站的链接 –