我们最近将我们的解决方案从Visual Studio 2010更新到VS 2017& .NET Framework 4.0到4.6.1。
在Postgres 9.4上运行的数据库没有任何变化。
这还包括将Npgsql DLL从版本2升级到版本3.
我们有一段代码如下:
using (MemoryStream memoryStream = new MemoryStream())
{
dataTable.WriteXml(memoryStream, XmlWriteMode.IgnoreSchema);
.
.
.
我们有一个PostgreSQL存储过程,它返回数组数据类型的列(string [],bool [],int []等。)。
在.NET 4.0中,上面的NpgSql 2版本工作正常,没有任何问题。但是使用新的设置,我们会收到序列化错误。
Type 'System.String[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' does not implement IXmlSerializable interface therefore can not proceed with serialization. at System.Data.XmlDataTreeWriter.XmlDataRowWriter(DataRow row, String encodedTableName)
at System.Data.XmlDataTreeWriter.Save(XmlWriter xw, Boolean writeSchema)
at System.Data.DataTable.WriteXml(XmlWriter writer, XmlWriteMode mode, Boolean writeHierarchy)
at System.Data.DataTable.WriteXml(Stream stream, XmlWriteMode mode, Boolean writeHierarchy)
我在VS调试器中注意到的主要事情是,这些数组列的DataType在VS 2017中显示为“System.Array”,但在之前(2010年)它显示的特定类型数组如“System.String [] ”
任何人都知道这里发生了什么?任何帮助/建议都非常感谢。
这是一个可用于复制问题的简单代码。
using System.Configuration;
using Npgsql;
using System.Data;
using System.IO;
namespace NpgSqlArrayTest
{
public class Program
{
public static void Main(string[] args)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString);
DataTable resultTable = new DataTable();
NpgsqlDataAdapter npgsqlDataAdapter = new NpgsqlDataAdapter("select * from \"FunctionReturnsArray\"();", npgsqlConnection);
npgsqlConnection.Open();
npgsqlDataAdapter.Fill(resultTable);
resultTable.TableName = "FunctionReturnsArray";
npgsqlConnection.Close();
using (MemoryStream memoryStream = new MemoryStream())
{
resultTable.WriteXml(memoryStream, XmlWriteMode.IgnoreSchema);
}
}
}
}
这是存储过程。
CREATE OR REPLACE FUNCTION "FunctionReturnsArray"()
RETURNS integer[] AS
$BODY$
declare
v_Count integer;
v_Array integer[];
begin
v_Count = 0;
while v_Count < 10 loop
v_Array = array_append(v_Array, v_Count);
v_Count = v_Count + 1;
end loop;
return v_Array;
end;
$BODY$
LANGUAGE plpgsql STABLE
COST 100;