将数据从文件复制到数据存储
Copy data from a file to a datastorage
是否可以导出插入查询?
我有疑问,在页面(http://www.filehelpers.net/)中的图像"Advance uses of the library: Copy data from a file to a datastorage"但是我找不到信息,不知道是否可以导出到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();
}
}
我敢肯定曾经有一些相关的文档——它们似乎在最新的网站上不见了。您可以查看 some more examples.
的 FileHelpers 源代码中的单元测试
是否可以导出插入查询?
我有疑问,在页面(http://www.filehelpers.net/)中的图像"Advance uses of the library: Copy data from a file to a datastorage"但是我找不到信息,不知道是否可以导出到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();
}
}
我敢肯定曾经有一些相关的文档——它们似乎在最新的网站上不见了。您可以查看 some more examples.
的 FileHelpers 源代码中的单元测试