我一直在尝试使用 OleDb 从 SQL Server 读取 XML 数据。
private static void Main(string[] args){
var con = new OleDbConnection("Provider=SQLNCLI11.1;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Temp");
var cmd = new OleDbCommand(
"SELECT [Id] ,[Description] FROM [Temp].[dbo].[SomeTable] where [Id]= 1 for xml path, root('root')", con);
con.Open();
byte[] result = null;
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read()){
result = (byte[]) reader[0];
}
MemoryStream stream = new MemoryStream(result);
stream.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(stream);
Console.Out.WriteLine(doc.OuterXml);
}
它没有说数据格式错误。如果我将字节数组转换为字符串,我会看到很多“奇怪”字符。我做错了什么?