【问题标题】:Display the latest updated data显示最新更新的数据
【发布时间】:2020-10-12 03:12:43
【问题描述】:

我必须在文本文件中显示当前正在更新的数据。问题是,我正在为需要显示的列使用CONVERT 函数。显示的数据是System.Byte[]。我需要显示的实际数据是一个varbinary 字符串。

-- This query is displaying the data before updating the status.
query = SELECT CONVERT(varchar(10), Start_RG, 2) AS Start_RG, CONVERT(varchar(10), End_RG, 2) AS End_RG, Status FROM RG WHERE Status = 'New';
command = new SqlCommand(query, cnn);

dR = command.ExecuteReader();

        if (dR.HasRows)
        {
            dR.Close();

            -- This is the query for updating the status to 'In Use'. I'm using the OUTPUT clause to display the data that has been update.
          sql = UPDATE TOP (1) RG SET Status = 'In Use' OUTPUT deleted.Start_RG, deleted.End_RG, deleted.Status WHERE Status = 'New';
          cmd= new SqlCommand(sql, cnn);

            dataReader = cmd.ExecuteReader();
            
             

            using (StreamWriter tw = File.AppendText(Path))
            {
                while (dataReader.Read())
                {
                    
                    tw.WriteLine("assign..\n");
                    tw.WriteLine("Start RG: {0}", Convert.Tostring((byte[])dataReader["Start_RG"]));
                    tw.WriteLine("End RG: {0}", Convert.Tostring((byte[])dataReader["End_RG"]));
                }
            }
                
}

如何获取当前将状态更新为“使用中”的Start_RGEnd_RG?我可以使用任何其他建议来代替OUTPUT 子句吗?

【问题讨论】:

  • 我已经更新了 C# 代码
  • 出现错误Incorrect syntax near the keyword 'CONVERT'
  • 好的,所以只需在 C# 中转换它,首先extract the byte array 然后convert to a string
  • 在调用writeline 之前,您必须将其完全转换为字符串。哈哈。或者至少将您的字符串转换代码放在writeline 调用中。
  • 对不起,我在这个问题中放错了代码。在编码中,我将代码放在 writeline 之前。显示的数据仍然是 System.Byte[].

标签: sql sql-server


【解决方案1】:

无论您使用 OUTPUT 子句做什么都可以。要读取 byte[] 到字符串,您可以使用以下方法。利用 Stackoverflow 回答:https://stackoverflow.com/a/4959269/634935

tw.WriteLine("Start RG: {0}", Encoding.ASCII.GetString(((byte[])dataReader["Start_RG"])); //based on encoding, you need to choose appropriate static method 
                    tw.WriteLine("End RG: {0}", Encoding.ASCII.GetString(((byte[])dataReader["End_RG"]));

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2015-10-15
    • 2023-04-06
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多