I'm running a SQLCLR function with SqlConnection("context connection=true")
.
In some cases, I get the exception
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
That happens every time with same query at same record number.
With connection
SqlConnection(@"Server=" + @"localhost\sqldeveloper16" + ";Database=" + db + ";Integrated Security=true;connect Timeout = 50")
I never get this error, and all is fine.
I don't understand why.
Then I tried same CLR on SQL Server 2008 and there are no problem...what do I have to check?!?!
Could this be a SQL Server 2016 bug?!
This my class
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;
}
}
}
and then from Sql
select *
from dbo.objdb('test','sys','all_columns',null,null)
result is (5248 records and last contains the exception, i posted only last 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
thank in advance!!
UPDATE:
i don't know why, but the problem is the column "collation_name
" ONLY ON SQL 2016.
i tried to remove it from the query and no exceptions...