【发布时间】: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_RG 和End_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