简体   繁体   English

从C#中的列值转义字符

[英]escape character from column value in c#

the following code read data from sqlserver2016 table and dump it in to a local folder. 以下代码从sqlserver2016表读取数据并将其转储到本地文件夹中。 but some column value contains comma which displace value into another column as delimiter I have used comma, so how can I keep the comma in the value but not displace value into another column. 但一些列值包含逗号它取代值到作为分隔符我用逗号另一列,所以我怎么能保持逗号价值,但不能取代值到另一列。 my code sample as below. 我的代码示例如下。 any help would be appreciated. 任何帮助,将不胜感激。

SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["db_connection"].AcquireConnection(Dts.Transaction) as SqlConnection);



            //Read data from table or view to data table
            string query = SQLQuery;
            SqlCommand cmd = new SqlCommand(query, myADONETConnection);
            //myADONETConnection.Open();
            DataTable d_table = new DataTable();
            d_table.Load(cmd.ExecuteReader());
            myADONETConnection.Close();



            string FileFullPath = DestinationFolder + "\\" + FileNamePart + "_" + datetime + FileExtension;

            StreamWriter sw = null;
            sw = new StreamWriter(FileFullPath, false);

            // Write the Header Row to File
            int ColumnCount = d_table.Columns.Count;
            for (int ic = 0; ic < ColumnCount; ic++)
            {
                sw.Write(d_table.Columns[ic]);
                if (ic < ColumnCount - 1)
                {
                    sw.Write(FileDelimiter);
                }
            }
            sw.Write(sw.NewLine);

            // Write All Rows to the File
            foreach (DataRow dr in d_table.Rows)
            {
                for (int ir = 0; ir < ColumnCount; ir++)
                {
                    if (!Convert.IsDBNull(dr[ir]))
                    {
                        sw.Write(dr[ir].ToString());
                    }
                    if (ir < ColumnCount - 1)
                    {
                        sw.Write(FileDelimiter);
                    }
                }
                sw.Write(sw.NewLine);

            }

            sw.Close();

            Dts.TaskResult = (int)ScriptResults.Success;

Wrap all your values in quotes. 将所有值都用引号引起来。 This escapes the comma's and should be understood by whatever you use to parse the file later on. 这转义为逗号,以后使用任何解析文件的方式都应理解。

The " is the default way of escaping in a csv. “”是转义csv的默认方式。

The following should do the trick. 以下应该可以解决问题。 This does assume your data does not contain any quotes. 这确实假定您的数据不包含任何引号。

sw.Write('"');
sw.Write(dr[ir].ToString());
sw.Write('"');

If your data does contain quotes you need to double those as shown below in order to escape them. 如果您的数据中确实包含引号,则需要将其加倍,如下所示,以便对其进行转义。

value1 | value2 | super"special"value 
"value1","value2","super""special""value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM