// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace System.Data.SqlClient.ManualTesting.Tests
{
    public class Bug98182
    {
        public static void Test(string constr, string dstTable)
        {
            string srctable = "[" + dstTable + " src]";
            dstTable = "[" + dstTable + "]";

            string[] epilogue = {
                "create table " + srctable + "([col 1] int primary key, [col 2] text)",
                "insert into " + srctable + " values (33, 'Michael')",
                "create table " + dstTable + "([col 1] int primary key, [col 2] text)",
            };
            string[] prologue = {
                "drop table  " + srctable,
                "drop table  " + dstTable,
            };

            using (SqlConnection dstConn = new SqlConnection(constr))
            using (SqlCommand dstCmd = dstConn.CreateCommand())
            {
                dstConn.Open();
                try
                {
                    Helpers.ProcessCommandBatch(typeof(SqlConnection), constr, epilogue);

                    using (SqlConnection srcConn = new SqlConnection(constr))
                    using (SqlCommand srcCmd = new SqlCommand(string.Format("select * from {0} ", srctable), srcConn))
                    {
                        srcConn.Open();

                        using (DbDataReader reader = srcCmd.ExecuteReader())
                        {
                            using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dstConn))
                            {
                                bulkcopy.DestinationTableName = dstTable;

                                SqlBulkCopyColumnMappingCollection ColumnMappings = bulkcopy.ColumnMappings;
                                ColumnMappings.Add("[col 1]", "col 1");
                                ColumnMappings.Add("col 2", "[col 2]");

                                bulkcopy.WriteToServer(reader);
                            }

                            Helpers.VerifyResults(dstConn, dstTable, 2, 1);
                        }
                    }
                }
                finally
                {
                    Helpers.ProcessCommandBatch(typeof(SqlConnection), constr, prologue);
                }
            }
        }
    }
}
