設計思路:數據導入到對應表需要單獨操作,每次操作的邏輯基本一致,所以操作邏輯可以放在基類中,子類繼承。設計如下:
using System; public interface IImportData { bool ImportData(); } public class SQLite2SqlServer : IImportData { public virtual bool ImportData() { // TODO: implement bool isSuccessful = true; dedestinationTableName = Destination; using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(SQL_SQLite)) using (System.Data.SqlClient.SqlBulkCopy bulkCopy = new System.Data.SqlClient.SqlBulkCopy(PubConstant.ConnectionString)) { { bulkCopy.DestinationTableName = Destination; try { bulkCopy.WriteToServer(reader); } catch (Exception ex) { isSuccessful = false; throw new Exception(ex.Message); } } } return isSuccessful; } public virtual bool Hook<T>() { // TODO: implement Type t = typeof(T); T dal = (T)Assembly.GetAssembly(t).CreateInstance(t.FullName); //XPWY.DAL.QuestionFace dal = new DAL.QuestionFace(); MethodInfo methodInfo = dal.GetType().GetMethod("GetList",new Type[]{typeof(string)}); DataSet ds = methodInfo.Invoke(dal, new String[] { " BatchID=" + this.BatchID.ToString() }) as DataSet; //GetList(" BatchID=" + this.BatchID.ToString()); if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { return true; } else { return false; } } public virtual bool ImportDataFromSQLiteToSQLServer<T>() { // TODO: implement if(Hook<T>) { return ImportData(); } return false; } public System.Int32 BatchID; public System.String Destination; public System.String SQL_SQLite; } public class QuestionFace : SQLite2SqlServer { public QuestionFace(System.Int32 nBatchID) { this.BatchID = nBatchID; this.Destination="QuetionFace"; this.SQL_SQLite="select * from QuestionFace"; } public sealed bool ImportDataFromSQLiteToSQLServer() { // TODO: implement return base.ImportDataFromSQLiteToSQLServer<QuestionFace>(); } }