0

I recently started using filehelpers.

FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
Customer[] customers = (Customer[]) engine.ReadFile(@"..\Data\CustomersDelimited.txt");

Instead of reading data from a file,I have to read data from SQL server table,then format the data and Write to file.

Please advise if any of you came with this scenario.

Codo
  • 75,595
  • 17
  • 168
  • 206
user1046415
  • 779
  • 4
  • 23
  • 43

2 Answers2

0

There is not much detail at all in your question. However, what I gather is that you are currently using

http://filehelpers.sourceforge.net/

to read/write data records from/to a flat file.

The easiest way to get started reading and writing a SQL Server database is with Entity Framework. Specifically use the Code First approach. It will be closest to what you are doing now.

Have a look at http://msdn.microsoft.com/en-us/data/ef.aspx to get started.

With Entity Framework Code First, you can read and write object instances from/to the database without much more complexity than what you are doing today with flat files.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

File Helpers provides an examples on the website.

The code looks like this (based on the linked example)

SqlServerStorage storage = new SqlServerStorage(typeof(OrdersVerticalBar)); 

storage.ServerName = "MyServerName"; 
storage.DatabaseName = "Northwind"; 

storage.FillRecordCallback = new FillRecordHandler(FillRecordOrder);   

The FillRecords method should look like this

protected void FillRecordOrder(object rec, object[] fields) 
{ 
    OrdersVerticalBar record = (OrdersVerticalBar) rec; 

    record.OrderID = (int) fields[0]; 
    record.CustomerID = (string) fields[1]; 
    record.EmployeeID = (int) fields[2]; 
    record.OrderDate = (DateTime) fields[3]; 
    record.RequiredDate = (DateTime) fields[4]; 

    if (fields[5] != DBNull.Value) 
         record.ShippedDate = (DateTime) fields[5]; 
    else 
         record.ShippedDate = DateTime.MinValue; 

    record.ShipVia = (int) fields[6]; 
    record.Freight = (decimal) fields[7]; 
} 

Then you can run the following to write the content directly to the output file.

FileDataLink.EasyExtractToFile(storage, "outfile.txt"); 
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Ya thanks for your reply. I had the same code as above..and was able to connect to SQl server. My requirement is to connect to Oracle DB.I found no class in filehelpers which i can use to connect to oracle DB. Please help if you have any ideas. Thanks – user1046415 Jul 30 '12 at 16:19
  • Your original question specifically refers to SQL Server. You should accept this answer and ask another question about Oracle. – shamp00 Jul 30 '12 at 16:47