Question: I have an abstract class public abstract class FileImporter
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Reflection;
namespace SomeNameSpace
{
///
/// This is deprecated. Do not use.
///
public class ColumnDefinition
{
///
/// Gets or sets the name of the column in the source file
///
public string ColumnName { get; set; }
///
/// Gets or sets the name of the destination property of the
/// import object
///
public string PropertyName { get; set; }
///
/// Gets or sets whether the column must exist in the source document.
///
public bool Required { get; set; }
}
public class ProcessItemEventArgs
{
private ImportItemResult _result;
private T _value;
public ProcessItemEventArgs(T value, ImportItemResult result)
{
this._result = result;
this._value = value;
}
public ImportItemResult Result
{
get { return _result; }
}
public T Item
{
get { return _value; }
}
}
public delegate void ProcessItemHandler
public abstract class FileImporter
{
private Dictionary
private FileType fileType = FileType.Unknown;
public event ProcessItemHandler
public FileImporter(ColumnDefinition[] columns)
{
if (columns == null)
throw new ArgumentNullException("columns");
if (columns.Length == 0)
throw new ArgumentException(String.Format(SR.InvalidArg_EmptyArray, "columns"));
// Copy the column definitions into a dictionary
_columns = columns.ToDictionary(x => x.ColumnName, StringComparer.OrdinalIgnoreCase);
}
protected virtual void OnProcessItem(ProcessItemEventArgs
{
if (ProcessItem != null)
ProcessItem(this, e);
}
public FileType FileType
{
get { return fileType; }
set { fileType = value; }
}
protected bool IsColumnAvailable(string columnName)
{
return _columns.ContainsKey(columnName);
}
private FileType DetermineFileType(string filename)
{
FileType fileType = FileType.Unknown;
FileInfo fi = new FileInfo(filename);
switch (fi.Extension.ToUpperInvariant())
{
case ".XLS": fileType = FileType.Excel; break;
case ".TXT": fileType = FileType.CSV; break;
case ".CSV": fileType = FileType.CSV; break;
}
return fileType;
}
private string GetConnectionString(string filename, FileType fileType)
{
string cn = "";
switch (fileType)
{
case FileType.CSV:
FileInfo fi = new FileInfo(filename);
cn = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text;HDR=Yes;FMT=Delimited\"", fi.DirectoryName);
break;
case FileType.Excel:
cn = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"", filename);
break;
case FileType.Unknown:
fileType = DetermineFileType(filename);
if (fileType == FileType.Unknown)
throw new InvalidOperationException("Cannot determine filetype");
cn = GetConnectionString(filename, fileType);
break;
}
return cn;
}
///
/// Upload catalog item text blocks from an excel workbook
///
/// The name of the file to import.
///
public ImportResult ImportFile(string fileName)
{
if (fileName == null)
throw new ArgumentNullException("fileName");
ImportResult result = new ImportResult();
var itemResults = new List
// load into oledb
string _cnstr = GetConnectionString(fileName, this.FileType);
using (OleDbConnection cn = new OleDbConnection(_cnstr))
{
cn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", cn);
using (OleDbDataReader rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
{
try
{
Type objectType = typeof(T);
var props = new Dictionary
var foundColumns = new Dictionary
for (int i = 0; i < rdr.FieldCount; i++)
{
var columnName = rdr.GetName(i);
ColumnDefinition col = null;
if (!_columns.TryGetValue(columnName, out col))
{
result.Success = false;
result.Message = String.Format("Unrecognized column ('{0}')", columnName);
return result;
}
foundColumns[col.ColumnName] = col;
props[col] = objectType.GetProperty(col.PropertyName);
_columns.Remove(columnName);
}
// Loop through remaining columns that were not found in the
// spreadsheet and determine if any were marked as required
string requiredColumns = "";
foreach (ColumnDefinition col in _columns.Values)
{
if (col.Required == true)
requiredColumns += ((requiredColumns.Length > 0 ? "," : "") + col.ColumnName);
}
if (requiredColumns.Length > 0)
{
result.Success = false;
result.Message = "Required columns " + requiredColumns + " missing";
return result;
}
_columns = foundColumns;
// Process records in spreadsheet
while (rdr.Read())
{
result.RecordCount++;
ImportItemResult itemResult = new ImportItemResult { RecordNumber = result.RecordCount };
T importObject = new T();
for (int i = 0; i < rdr.FieldCount; i++)
{
ColumnDefinition col = _columns[rdr.GetName(i)];
object value = rdr.GetValue(i);
PropertyInfo prop = props[col];
object convertedValue = Convert.ChangeType(value, prop.PropertyType);
prop.SetValue(importObject, convertedValue, null);
}
OnProcessItem(new ProcessItemEventArgs
if (itemResult.Processed == true)
{
result.RecordsProcessed++;
}
else
{
itemResults.Add(itemResult);
}
}
}
catch (Exception ex)
{
result.Success = false;
result.Message = ex.Message;
}
}
}
result.Items = itemResults.ToArray();
return result;
}
}
}
Nilanka DharmadasaPosted Nov 4, 2009, 11:57 PM
You cannot instatiate from an abstract class.
You should create enother calss which extends from this abstarct class.
public class Myclass : FileImporter
{
public Myclass()
{
}
}
Then you can instantiate from this class and call ImportFile method.
Muclass obj = new Myclass();
obj.ImportFile ();
If you find this answer useful, please do not forget to accpet this answer.