MONO CS的表值参数问题 [英] Table Valued Parameter issue with MONO cs

查看:70
本文介绍了MONO CS的表值参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码为表值类型创建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编译器给我这个错误:

Mono compiler giving me this error:

Error CS1061: Type `System.Data.SqlClient.SqlParameter' does not contain a definition for `TypeName' and no extension method `TypeName' of type `System.Data.SqlClient.SqlParameter' could be found. Are you missing an assembly reference? (CS1061)

给定的代码只是试图为TableValued Type创建一个参数,并将数据表传递给SQL插入语句.

The given code is simply trying to create a parameter for TableValued Type and pass data table to SQL insert statement.

我知道如果使用存储过程可以解决该错误,但是在我的情况下,为每个表创建MERGE插入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:

The table type parameter '@drFactory' must have a valid type name.

The table type parameter '@drFactory' must have a valid type name.

推荐答案

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的表值参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆