Beispiel #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);
        }
Beispiel #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 System.Exception("data schema missed!");
            }
            if (context.SuccessCount == 0)
            {
                throw new System.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);
                }
        }