DarkWanderer / ClickHouse.Client

.NET client for ClickHouse
MIT License
303 stars 58 forks source link

how to use bulkcopy #443

Closed chenhunhun closed 3 months ago

chenhunhun commented 4 months ago

when i try to migrate the sqlserver data to clickhouse,

        string sqlConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
        string clickhouseConnectionString = "Host=localhost;Database=my_clickhouse_db";

        using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
        {
            sqlConn.Open();

            using (ClickHouseBulkCopy clickhouseBc = new ClickHouseBulkCopy(clickhouseConnectionString))
            {
                clickhouseBc.DestinationTableName = "my_clickhouse_table";

                using (SqlCommand cmd = new SqlCommand("SELECT * FROM my_sql_table", sqlConn))
                {
                    clickhouseBc.WriteToServer(cmd.ExecuteReader());
                }
            }
        }

i get error: “ClickHouseBulkCopy.get_DestinationTableName()” or “ClickHouseBulkCopy.set_DestinationTableName(?)” ConsoleApp1 C:\Users\Administrator\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 24
and WriteToServer function is not exist.

MikeAmputer commented 4 months ago

@chenhunhun you get these errors because DestinationTableName is init only (since 7.0.0 probably) and write to server function is WriteToServerAsync(IDataReader, CancellationToken). Also you need to call InitAsync() first. You can find an example here.