【发布时间】:2018-01-27 05:27:41
【问题描述】:
我正在使用SqlConnection("context connection=true") 运行 SQLCLR 函数。
在某些情况下,我得到了异常
System.Data.SqlClient.SqlException (0x80131904)
在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection,Action`1 wrapCloseInAction)
在 System.Data.SqlClient.SqlInternalConnectionSmi.EventSink.DispatchMessages(布尔忽略非致命消息)
在 System.Data.SqlClient.SqlDataReaderSmi.InternalRead(布尔忽略非致命错误)
在 ObjDb.Functions.ObjDb(String db, String schema, String obj, String col, String val)ClientConnectionId:00000000-0000-0000-0000-000000000000
错误号:200,状态:4,类:25
每次使用相同的查询在相同的记录号时都会发生这种情况。
有连接
SqlConnection(@"Server=" + @"localhost\sqldeveloper16" + ";Database=" + db + ";Integrated Security=true;connect Timeout = 50")
我从来没有遇到过这个错误,一切都很好。
我不明白为什么。
然后我在 SQL Server 2008 上尝试了相同的 CLR,没有问题...我需要检查什么?!?!
这可能是 SQL Server 2016 的错误吗?!
这是我的课
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Xml;
using Newtonsoft.Json;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Text;
namespace ObjDb
{
public partial class Functions
{
[SqlFunction
(
//DataAccess = DataAccessKind.Read,//serve x accedere alle tabelle del db, altrimenti accede solo a qlle di sistema...
SystemDataAccess = SystemDataAccessKind.Read,
FillRowMethodName = "columns_ok",
TableDefinition = "c1 nvarchar(max), node nvarchar(max)"
)
]
public static IEnumerable ObjDb(String db, String schema, String obj, String col, String val)
{
List<String> rows = new List<String>();//List<Object[]> rows = new List<Object[]>();
//List<tuple.t2<String, String>> rows = new List<tuple.t2<String, String>>();
SqlCommand command = null;// = new SqlCommand(query, conn);
SqlConnection conn = null;// new SqlConnection("context connection=true");
try
{
conn = new SqlConnection("context connection=true");//new SqlConnection(@"Server=" + @"localhost\sqldeveloper16" + ";Database=" + db + ";Integrated Security=true;connect Timeout = 50"); //new SqlConnection("context connection=true");//
conn.Open();
String query;
StringBuilder sb = new StringBuilder();
sb.Append("where 0=0");
if ((col != null && !col.Equals("")) && (val != null && !val.Equals("")))
{
String[] cols = col.Split(',');
String[] vals = val.Split(',');
for (int i = 0; i < Math.Min(cols.Length, vals.Length); i++)
{
sb.Append(" and [").Append(cols[i]).Append("]='").Append(vals[i].Replace("'", "''")).Append("'");
}
//filter = "where 0=0 " + sb.ToString();
//filter = "where [" + col + "]='" + val + "'";
}
//estrazione inline dell xml (un xml x ogni riga)
//"BINARY BASE64" -> https://stackoverflow.com/questions/8801697/xml-export-via-bcp-bulk-export - bug 2008R2, converte il varbinary in ascii
query =
"select (select t.* for xml raw('root'),BINARY BASE64) " +
"from [" + db + "].[" + schema + "].[" + obj + "] t " +
sb.ToString(); ;//filter;
command = new SqlCommand(query, conn);
//command.CommandTimeout = 0;
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
//rows.Add(new tuple.t2<String, String>("", (String)dr[0]));
rows.Add((String)dr[0]);
}
return rows;
}
catch (Exception e)
{
rows.Add(e.ToString().Substring(0, Math.Min(4000, e.ToString().Length)));
return rows;
}
finally
{
if (command != null)
command.Dispose();
if (conn != null)
conn.Close();
}
}
private static void columns_ok(object resultObj, out SqlString node)
{
//String res = (String)resultObj;
node = (String)resultObj;
}
}
}
然后从 Sql
select *
from dbo.objdb('test','sys','all_columns',null,null)
结果是(5248 条记录,最后包含异常,我只发布了最后 3 条):
<root object_id="-103402673" name="similarity_index_page_count" column_id="4" system_type_id="127" user_type_id="127" max_length="8" precision="19" scale="0" is_nullable="1" is_ansi_padded="0" is_rowguidcol="0" is_identity="0" is_computed="0" is_filestream="0" is_replicated="0" is_non_sql_subscribed="0" is_merge_published="0" is_dts_replicated="0" is_x005F_xml_document="0" xml_collection_id="0" default_object_id="0" rule_object_id="0" is_sparse="0" is_column_set="0" generated_always_type="0" generated_always_type_desc="NOT_APPLICABLE" is_hidden="0" is_masked="0"/>
<root object_id="-103085222" name="database_id" column_id="1" system_type_id="56" user_type_id="56" max_length="4" precision="10" scale="0" is_nullable="0" is_ansi_padded="0" is_rowguidcol="0" is_identity="0" is_computed="0" is_filestream="0" is_replicated="0" is_non_sql_subscribed="0" is_merge_published="0" is_dts_replicated="0" is_x005F_xml_document="0" xml_collection_id="0" default_object_id="0" rule_object_id="0" is_sparse="0" is_column_set="0" generated_always_type="0" generated_always_type_desc="NOT_APPLICABLE" is_hidden="0" is_masked="0"/>
System.Data.SqlClient.SqlException (0x80131904) at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnectionSmi.EventSink.DispatchMessages(Boolean ignoreNonFatalMessages) at System.Data.SqlClient.SqlDataReaderSmi.InternalRead(Boolean ignoreNonFatalErrors) at ObjDb.Functions.ObjDb(String db, String schema, String obj, String col, String val) ClientConnectionId:00000000-0000-0000-0000-000000000000 Error Number:200,State:4,Class:25
提前致谢!!
更新:
我不知道为什么,但问题是“collation_name”列仅在 SQL 2016 上。
我试图从查询中删除它,没有例外......
【问题讨论】:
-
您能否发布您的 SQLCLR 代码以及完整的错误消息。错误 0x80131904 可能是几件事,一个是找不到服务器,但也可能是其他错误。另外,“相同记录号的相同查询”是什么意思(我理解相同的查询但相同的记录号???)。
-
所以,clr 每次为该查询给我一些记录(例如 5249),然后抛出异常。我尝试了 2 sql 2016 和同样的问题,2008r2 一切都很好......
-
它正在执行什么确切查询?请至少将该查询发布到问题中,但最好还包括 .NET 代码。
-
没有过多研究,我认为您的错误处理掩盖了实际错误。
-
这是 .net 给我的...我认为是服务器设置,发生在 sql 2016 istance 上,而不是在 2008 上。我尝试了 3 个 sql 2016 实例,仅在这些实例上给出错误。 .我不知道我要检查什么...
标签: .net sql-server user-defined-functions sqlclr