繁体   English   中英

C# 语法帮助、日期格式和向字符串添加 "

[英]C# syntax help, date formatting and adding " to strings

这是我作为 SSIS 和 Informatica 开发人员第一次涉足 C#,只使用 SQL。 我有一个脚本任务,它通过查询从单个 SQL Server 表中读取数据,然后将该数据简单地写入文本文件。 一切正常,除了我认为是我无法弄清楚的两个小格式问题。

此构建具有以下要求。 预先感谢我在这里回答任何问题!

  1. SQL 查询被有意设置为 Select * 以获取添加的任何新列(已在代码中)
  2. 从写入文件中排除的前 3 列(已在代码中)

问题:

  1. " " 包装器需要添加到所有值、列和行。
  2. 数据库中的日期为真日期,但写入文件时显示日期时间。 需要只是日期。

当前的:

ID 姓名 日期 比率
12345678 约翰·韦恩 12/31/2018 12:00:00 AM 1/1

需要是:

“ID” “姓名” “日期” “比率”
“12345678” “约翰·韦恩” 《2018-12-31》 “1/1”

代码:

// Declare Variables
string DestinationFolder = Dts.Variables["User::Target_FilePath"].Value.ToString();
string QueryStage = Dts.Variables["User::Query_Stage"].Value.ToString();
//string TableName = Dts.Variables["User::TableName"].Value.ToString();
string FileName = Dts.Variables["User::OutputFileName"].Value.ToString();
string FileDelimiter = Dts.Variables["User::Target_FileDelim"].Value.ToString();
//string FileExtension = Dts.Variables["User::AC_Prefix"].Value.ToString();

//USE ADO.NET Connection from SSIS Package to get data from table
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["ADO_TEST_CONN"].AcquireConnection(Dts.Transaction) as SqlConnection);

// Read data from table or view to data table
string query = QueryStage;

SqlCommand cmd = new SqlCommand(query, myADONETConnection);
//myADONETConnection.Open();

DataTable d_table = new DataTable();
d_table.Load(cmd.ExecuteReader());
myADONETConnection.Close();

string FileFullPath = DestinationFolder + "\\" + FileName + ".txt";

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

// Write the Header Row to File
int ColumnCount = d_table.Columns.Count;

for (int ic = 4; 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 = 4; 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;

object上盲目运行.ToString() ,这是在sw.Write(dr[ir].ToString()); , 将使用将该数据类型转换为字符串的默认设置。 如果它是DateTime (c# 数据类型,而不是 SQL Date列类型),那么它将包含时间信息。

C# 将 SQL 列类型(例如Date )转换为 C# 数据类型( DateTime )。 您需要检测这一点,就像检测值是否为 DBNull 一样。

if (!Convert.IsDBNull(dr[ir]))
{
    if (dr[ir] is DateTime dt)
    {
        // use DateTime's specific string rendering
        sw.Write(dt.ToString("d"));
    }
    else
    {
        // fall back to standard string rendering
        sw.Write(dr[ir].ToString());
    }
}

如果您需要不同的格式,您可以将格式(在本例中为"d" )更改为其他格式。 请记住,计算机的文化会影响字符串的呈现方式,除非您明确使用命名的文化。


问题中的另一件事是在打印值周围添加引号。 这可以通过字符串连接来完成。 例如:

string result = "\"" + "my string" + "\"";
// result is "my string", with quotes

记住要转义引号。

暂无
暂无

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

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