设计思路:数据导入到对应表需要单独操作,每次操作的逻辑基本一致,所以操作逻辑可以放在基类中,子类继承。设计如下:
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>(); } }