Exemple #1
0
        internal override void CreateDataSchema(IntegrationContext context)
        {
            string connectionstring = context.State[ContextState.DatabaseConnectionString] as string;

            if (string.IsNullOrEmpty(connectionstring))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.DatabaseConnectionString));
            }
            string tableName = context.State[ContextState.DatabaseTableName] as string;

            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.DatabaseTableName));
            }

            DataTable dt = new DataTable(tableName);

            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                conn.Open();

                SqlCommand     cmd2    = new SqlCommand(string.Format("SELECT * FROM {0} WHERE 1<>1;", tableName), conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd2);
                adapter.Fill(dt);
            }
            context.Schema = DataTableStorage.GetDataSchema(dt);
        }
Exemple #2
0
        internal override void TransferData(IntegrationContext context)
        {
            string connectionstring = context.State[ContextState.DatabaseConnectionString] as string;

            if (string.IsNullOrEmpty(connectionstring))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.DatabaseConnectionString));
            }
            if (null == context.Schema)
            {
                throw new Exception("data schema missed!");
            }
            if (context.SuccessCount == 0)
            {
                throw new Exception("data missed!");
            }
            string tableName = context.State[ContextState.DatabaseTableName] as string;

            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.DatabaseTableName));
            }
            bool isTruncate = (bool)context.State[ContextState.DatabaseTruncateTable];

            if (isTruncate)
            {
                TruncateData(connectionstring, tableName);
            }

            using (SqlConnection conn = new SqlConnection(connectionstring))
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
                {
                    conn.Open();

                    DataTable      dt      = new DataTable(tableName);
                    SqlCommand     cmd2    = new SqlCommand(string.Format("SELECT * FROM {0} WHERE 1<>1;", tableName), conn);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd2);
                    adapter.Fill(dt);
                    MappingColumns(bulkCopy, dt, context.Schema);
                    DataTableStorage.FillDestinationDataItems(dt, context.Data);
                    bulkCopy.DestinationTableName = tableName;
                    bulkCopy.BatchSize            = dt.Rows.Count;
                    bulkCopy.WriteToServer(dt);
                }
        }
Exemple #3
0
        internal override void GetData(IntegrationContext context)
        {
            string sourcePath = context.State[ContextState.SourcePath] as string;

            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.SourcePath));
            }
            if (!File.Exists(sourcePath))
            {
                throw new FileNotFoundException(string.Format("File [{0}] not found!", sourcePath));
            }

            if (null == context.Schema)
            {
                throw new ArgumentException("data schema missed!");
            }
            string tableName = context.State["tableName"] as string;

            using (ExcelReader reader = new ExcelReader(sourcePath))
            {
                string[] tables = reader.GetExcelTableNames();
                // it is vary possibly that csv data but filed end with xls. we need parse in csv format
                if (tables == null)
                {
                    throw new InvalidDataException(string.Format("[{0}]不是有效的excel文件", sourcePath));
                }
                if (tables.Length == 0)
                {
                    throw new ArgumentException("无法找到excel页", sourcePath);
                }
                if (tableName == null ||
                    !Array.Exists(tables,
                                  delegate(string t) { return(t.Equals(tableName, StringComparison.InvariantCultureIgnoreCase)); }))
                {
                    tableName = tables[0];
                }

                DataTable dt = reader.GetTable(tableName);
                DataTableStorage.GetDataFromSourceDataTable(dt, context);
            }
        }
Exemple #4
0
        internal override void TransferData(IntegrationContext context)
        {
            string desDirectory = context.State["desDirectory"] as string;

            if (string.IsNullOrEmpty(desDirectory))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", desDirectory));
            }
            if (!Directory.Exists(desDirectory))
            {
                throw new FileNotFoundException(string.Format("Directory [{0}] does not exist!", desDirectory));
            }

            if (null == context.Schema)
            {
                throw new ArgumentException("没有找到可用的数据结构!");
            }
            if (context.Data.Count == 0)
            {
                throw new ArgumentException("没有找到可用的数据!");
            }

            DataTable dt = DataTableStorage.GetEmptyDataTable(context.Schema);

            foreach (DataItem item in context.Data)
            {
                DataTableStorage.PrepareDataForDestination(item, dt);
            }

            string file, message;

            if (!ExcelReader.SaveData(desDirectory, dt, out file, out message))
            {
                // TODO: fails handle here
                context.Status = IntegrationStatus.Failure;
            }
            else
            {
                context.State["desFile"] = file;
            }
        }