Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TableauHyperApi write wrong value to hyper file #126

Open
feihuang-personal opened this issue Sep 23, 2024 · 0 comments
Open

TableauHyperApi write wrong value to hyper file #126

feihuang-personal opened this issue Sep 23, 2024 · 0 comments

Comments

@feihuang-personal
Copy link

Dear Team,

I meet a inexplainable problem with TableHyperApi(Tableau_DataExtractHyper_DotNet.dll, version 1.0.21.0). Could you please give me some suggestions how can I fix the problem? Thanks so much.

I'm using below code(C#,NET Framework4.6.1) to create a hyper file and trying to write data from a DataReader. I promise the data in DataReader is absolutely correct. There're data in DataReader is just show as below:

Column A(string) Column B(string) ColumnC(integer)
Test Value A Test Value B 11111

However, the data are written to hyper file is just show as below:

Column A(string) Column B(string) ColumnC(integer)
11008

You can see that the String value are empty and the integer value is not the same with original value. But, this issue only occurs on one server, for another one it works fine and the data in hyper file are correct(both these two servers have the same configuration and software installed, include vistual C++ redistributable version). And on my own personal computer, it also works fine. So currently I don't what happened for this server, hope you can share me any solution or suggestions, thanks.

connection.Open(); 

using (var cmd = new SqlCommand(sql, connection))
{
    var reader = cmd.ExecuteReader();

    var path = @"path\to\Tableau-ExtractAPI64Hyper\bin";

    var desktop = @"path\to\resultfolder";
    new HyperGenerator(path).GenerateHyperFile(path, desktop, "test.hyper", reader);
}

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using TableauHyper;
using static System.Net.Mime.MediaTypeNames;

namespace ConsoleApp1
{

    public class HyperGenerator
    {
        public HyperGenerator(string apiLocation)
        {
            DataExtractHyperAPILocation.TDE64Folder = apiLocation;
        }

        public void GenerateHyperFile(string apiLocation, string extractPath, string extractFileName, SqlDataReader data)
        {
            string text = "Extract";
            try
            {
                if (!data.HasRows)
                {
                    throw new Exception("NO ROWS");
                }

                string path = Path.Combine(extractPath, extractFileName);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (ExtractHyper extractHyper = new ExtractHyper(path))
                {
                    List<string> list = new List<string>();
                    using (TableDefinition tableDefinition = new TableDefinition())
                    {
                        for (int i = 0; i < data.FieldCount; i++)
                        {
                            string name = data.GetName(i);
                            TableauHyper.Type type = GetType(data.GetFieldType(i).Name);
                            tableDefinition.AddColumn((!list.Contains(name)) ? name : (name + "1"), type);
                            list.Add(data.GetName(i));
                        }

                        Table table = extractHyper.AddTable(text, tableDefinition);
                        int num = 0;
                        while (data.Read())
                        {
                            Row row = new Row(tableDefinition);
                            for (int j = 0; j < tableDefinition.GetColumnCount(); j++)
                            {
                                var columnName = tableDefinition.GetColumnName(j);
                                var columnType = tableDefinition.GetColumnType(j);
                                var columnValue = data.GetValue(j).ToString();
           
                               // I can see the data from DataReader is correct via below output.
                                Console.WriteLine($"Column:{columnName},Type:{columnType}, Value:{columnValue}");

                                switch (tableDefinition.GetColumnType(j))
                                {
                                    case TableauHyper.Type.Type_Boolean:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            row.SetBoolean(j, Convert.ToBoolean(data.GetValue(j)));
                                        }

                                        break;
                                    case TableauHyper.Type.Type_CharString:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            row.SetCharString(j, data.GetValue(j).ToString());
                                        }

                                        break;
                                    case TableauHyper.Type.Type_Date:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            DateTime dateTime3 = DateTime.Parse(data.GetValue(j).ToString());
                                            row.SetDate(j, dateTime3.Year, dateTime3.Month, dateTime3.Day);
                                        }

                                        break;
                                    case TableauHyper.Type.Type_DateTime:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            DateTime dateTime = DateTime.Parse(data.GetValue(j).ToString());
                                            row.SetDateTime(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
                                        }

                                        break;
                                    case TableauHyper.Type.Type_Double:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            row.SetDouble(j, Convert.ToDouble(data.GetValue(j).ToString()));
                                        }

                                        break;
                                    case TableauHyper.Type.Type_Duration:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            DateTime dateTime2 = DateTime.Parse(data.GetValue(j).ToString());
                                            row.SetDuration(j, dateTime2.Day, dateTime2.Hour, dateTime2.Minute, dateTime2.Second, dateTime2.Millisecond);
                                        }

                                        break;
                                    case TableauHyper.Type.Type_Integer:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            row.SetInteger(j, Convert.ToInt32(data.GetValue(j).ToString()));
                                        }

                                        break;
                                    case TableauHyper.Type.Type_UnicodeString:
                                        if (data.GetValue(j) != DBNull.Value)
                                        {
                                            row.SetString(j, data.GetValue(j).ToString());
                                        }

                                        break;
                                }
                            }

                            table.Insert(row);

                            num++;
                        }
                    }
                }
            }
            catch (TableauException ex)
            {
                throw new Exception($"{ex.GetResultCode()}: {ex.Message}");
            }
            catch (Exception ex2)
            {
                throw new Exception(ex2.Message);
            }
            finally
            {
                data.Close();
                data.Dispose();
            }
        }

        private TableauHyper.Type GetType(string type)
        {
            TableauHyper.Type result = (TableauHyper.Type)0;
            switch (type.ToLower())
            {
                case "int32":
                case "int64":
                case "byte":
                    result = TableauHyper.Type.Type_Integer;
                    break;
                case "float":
                case "decimal":
                    result = TableauHyper.Type.Type_Double;
                    break;
                case "bool":
                case "boolean":
                    result = TableauHyper.Type.Type_Boolean;
                    break;
                case "date":
                    result = TableauHyper.Type.Type_Date;
                    break;
                case "time":
                    result = TableauHyper.Type.Type_Duration;
                    break;
                case "datetime":
                case "timespan":
                    result = TableauHyper.Type.Type_DateTime;
                    break;
                case "char":
                    result = TableauHyper.Type.Type_CharString;
                    break;
                case "string":
                case "guid":
                    result = TableauHyper.Type.Type_UnicodeString;
                    break;
            }

            return result;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant