以下代碼從sqlserver2016表讀取數據並將其轉儲到本地文件夾中。但某些列值包含逗號,將值替換爲另一列作爲分隔符,我使用了逗號,所以如何將逗號保留在值中但不會將值替換爲另一列中的值。我的代碼示例如下。任何幫助,將不勝感激。c#中列值的轉義字符
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;
可是你用位移意思?你可以給輸出你得到你想要得到的一個? – Doruk
數據庫示例中的值,列名稱:數據和值:「This is,sample」。但是當我把它寫入csv文件的值分成兩個單獨的列,「這是」和「樣本」,我想整個值「這是樣本」應該在一列csv – Sumon
你不能使用雙報價,如果它有價值,在它? –