问题描述
我有一个简单的代码来为表值类型创建 SqlParameter.给定的代码适用于 .NET 4.0.问题在于 MONO CS (3.12.0),我不能简单地在 MONO 中编译相同的代码.
I have a simple code to create SqlParameter for Table Valued Type. The given code works just fine with .NET 4.0. Issue is with MONO CS (3.12.0), I cannot simply compile the same code in MONO.
static SqlParameter GetDataTableParam(string _tableName, DataTable _dt)
{
SqlParameter tValue = new SqlParameter();
tValue.ParameterName = "@dr" + _tableName; //@drFactory
tValue.SqlDbType = SqlDbType.Structured;
tValue.Value = _dt;
tValue.TypeName = string.Format("dbo.{0}Item", _tableName); //MONO CS is giving error at this line
return tValue;
}
单声道编译器给我这个错误:
Mono compiler giving me this error:
错误 CS1061:System.Data.SqlClient.SqlParameter"类型不包含TypeName"的定义,并且找不到System.Data.SqlClient.SqlParameter"类型的扩展方法TypeName".您是否缺少程序集参考?(CS1061)
给定的代码只是尝试为 TableValued 类型创建一个参数并将数据表传递给 SQL 插入语句.
The given code is simply trying to create a parameter for TableValued Type and pass data table to SQL insert statement.
我知道如果我使用存储过程可以解决错误,但在我的情况下,为每个表创建 MERGE insert SP 是不可行的.
I know the error can be resolved if I use stored procedure, but in my case its no feasible to create MERGE insert SP for each and every table.
因此,如果有任何解决此问题的方法,请帮助我.
So please help me if there is any work around of this issue.
注意:已知MONO System.Data.SqlClient.SqlParameter
没有TypeName
属性.如果我删除此属性,则它编译得很好,但会出现运行时错误:
Note: It is known that MONO System.Data.SqlClient.SqlParameter
does not have TypeName
property. If I remove this property then it compiles fine but gives run time error:
表类型参数@drFactory"必须具有有效的类型名称.
推荐答案
MONO SqlParameter
类没有暴露 TypeName
属性,但它在源代码中.
MONO SqlParameter
class does not expose TypeName
property but it is there in the source code.
所以,我使用 Reflection
将值设置为 TypeName
属性:
So, I have used Reflection
to set value to TypeName
property:
SqlParameter tValue = new SqlParameter("@dr" + _tableName, _dt);
tValue.SqlDbType = SqlDbType.Structured;
System.Reflection.PropertyInfo propertyInfo = tValue.GetType().GetProperty("TypeName");
propertyInfo.SetValue(tValue, "dbo.factoryItem", null);
这篇关于MONO cs 的表值参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!