public override ImportResult[] ImportData(ImportStatement[] statements)
        {
            List<ImportResult> importResults = new List<ImportResult>();

            string connectionString = ConnectionStringMaker.SQLServerConnectionString(config.ConnectionSetup);

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                foreach (ImportStatement s in statements)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = s.SqlStatement;
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = sqlConnection;

                    try
                    {
                        int rowsAffected = cmd.ExecuteNonQuery();
                        importResults.Add(new SuccesfulImport(s, rowsAffected));
                    }
                    catch (SqlException e)
                    {
                        importResults.Add(new UnsuccesfulImport(s, e.Message));
                    }

                }
            }

            return importResults.ToArray();
        }
        public void UnsuccesfulImportTest()
        {
            ImportStatement statement = new ImportStatement("'a'", "0");
            SQLServerDataImporter importer = new SQLServerDataImporter(getConfig());
            ImportResult result = importer.ImportData(statement);

            UnsuccesfulImport import = (UnsuccesfulImport)result;
            Assert.AreEqual("Incorrect syntax near 'a'.", import.ErrorMsg);
            Assert.AreEqual(statement, import.Statement);
        }
        public void SuccesfulImportTest()
        {
            ImportStatement statement = new ImportStatement("declare @test table (id int) insert into @test values (0)", "0");
            SQLServerDataImporter importer = new SQLServerDataImporter(getConfig());
            ImportResult result = importer.ImportData(statement);

            SuccesfulImport import = (SuccesfulImport)result;
            Assert.AreEqual(1, import.RowsAffected);
            Assert.AreEqual(statement, import.Statement);
        }
        public void TimeoutTest()
        {
            string hostNameDoesntExist = "testest";
            ConnectionSetup connectionSetup = new ConnectionSetup(hostNameDoesntExist, "", "", true);
            connectionSetup.Timeout = 1;

            ImportConfiguration config = new ImportConfiguration(null, connectionSetup, "", null);

            ImportStatement[] statements = new ImportStatement[0];

            SQLServerDataImporter importer = new SQLServerDataImporter(config);

            importer.ImportData(statements);
        }
        public void MultipImportsTest()
        {
            ImportStatement statement1 = new ImportStatement("declare @test table (id int) insert into @test values (0)", "0");
            ImportStatement statement2 = new ImportStatement("declare @test table (id int) insert into @test values (0) insert into @test values (0)", "1");
            ImportStatement statement3 = new ImportStatement("'a'", "2");
            SQLServerDataImporter importer = new SQLServerDataImporter(getConfig());

            ImportResult[] result = importer.ImportData(new ImportStatement[] { statement1, statement2, statement3 });

            SuccesfulImport import1 = (SuccesfulImport)result[0];
            SuccesfulImport import2 = (SuccesfulImport)result[1];
            UnsuccesfulImport import3 = (UnsuccesfulImport)result[2];

            Assert.AreEqual(1, import1.RowsAffected);
            Assert.AreEqual(statement1, import1.Statement);
            Assert.AreEqual(2, import2.RowsAffected);
            Assert.AreEqual(statement2, import2.Statement);
            Assert.AreEqual("Incorrect syntax near 'a'.", import3.ErrorMsg);
            Assert.AreEqual(statement3, import3.Statement);
        }
        public override ImportResult ImportData(ImportStatement statement)
        {
            string connectionString = ConnectionStringMaker.SQLServerConnectionString(config.ConnectionSetup);

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = statement.SqlStatement;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlConnection;

                try
                {
                    int rowsAffected = cmd.ExecuteNonQuery();
                    return new SuccesfulImport(statement, rowsAffected);
                }
                catch (SqlException e)
                {
                    return new UnsuccesfulImport(statement, e.Message);
                }
            }
        }
 public abstract ImportResult ImportData(ImportStatement statement);
 public abstract ImportResult[] ImportData(ImportStatement[] statements);
 public UnsuccesfulImport(ImportStatement statement, string errorMsg)
     : base(statement)
 {
     this.errorMsg = errorMsg;
 }
Esempio n. 10
0
 public SuccesfulImport(ImportStatement statement, int rowsAffected)
     : base(statement)
 {
     this.rowsAffected = rowsAffected;
 }
Esempio n. 11
0
 public ImportResult(ImportStatement statement)
 {
     this.statement = statement;
 }