ssis generate json file remove return

天大地大妈咪最大 提交于 2019-12-24 01:09:20

问题


I am using a script task to generate a json file from a sql query.

The c# code in the script task:

public void Main()
{
    // TODO: Add your code here

    ConnectionManager cm;
    string sqlString = "";

    System.IO.StreamWriter file = new System.IO.StreamWriter(@"f:\JSONOutput.txt");

    sqlString = "SELECT * FROM[dbo].[JJVCACUProductElectron] where id in (1,2,3) for json auto";
    System.Data.SqlClient.SqlConnection sqlConn;
    System.Data.SqlClient.SqlCommand sqlComm;

    cm = Dts.Connections["crm_vm_2017_cs_dotnet"];

    sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
    sqlComm = new System.Data.SqlClient.SqlCommand(sqlString, sqlConn);
    System.Data.SqlClient.SqlDataReader reader = sqlComm.ExecuteReader();
    try
    {
while (reader.Read())
{

    file.WriteLine(reader[0]);
}
    }
    finally
    {
// Always call Close when done reading.
reader.Close();
    }

    cm.ReleaseConnection(sqlConn);
    Dts.TaskResult = (int)ScriptResults.Success;
}

The generated output file is incomplete, I guess there is probably a return in some column. How to remove the return characters in the output ?


回答1:


Workaround

You can remove the for json auto part from the SQL command and import data into a DataTable, then export the DataTable to Json file.

Import data into DataTable

  • Read SQL Table into C# DataTable

Export DataTable to Json file

  • Convert datatable to JSON in C#



回答2:


The working code: it seems sql server when sending out data from the select ... for json auto will automatically spit rows at certain size. But there is no crlf at the end of the row. So the file.write method can continuously concatenate the strings to form one big single line string in the file, which is valid json file. My previous code used writeline, which probably added crlf at the end of each string. And also the key is the using (file) part, otherwise can see the output file got truncated, which is the problem in my original post.

        public void Main()
        {
            ConnectionManager cm;

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"e:\jsontest"))
            {
                System.Data.SqlClient.SqlConnection sqlConn;
                System.Data.SqlClient.SqlCommand sqlComm;

                cm = Dts.Connections["crm_vm_2017_cs_dotnet"];

                sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
                sqlComm = new System.Data.SqlClient.SqlCommand("select * from JJVCProduct for json auto", sqlConn);
                System.Data.SqlClient.SqlDataReader reader = sqlComm.ExecuteReader();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                try
                {
                    while (reader.Read())
                    {
                        file.Write(reader.GetValue(0).ToString());
                    }

                }
                finally
                {
                    reader.Close();
                }

                cm.ReleaseConnection(sqlConn);
                Dts.TaskResult = (int)ScriptResults.Success;
            }


        }


来源:https://stackoverflow.com/questions/54059599/ssis-generate-json-file-remove-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!