Table Valued Parameter issue with MONO cs

橙三吉。 提交于 2019-12-04 05:52:06

问题


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:

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)

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

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.

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.


回答1:


MONO SqlParameter class does not expose TypeName property but it is there in the source code.

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);


来源:https://stackoverflow.com/questions/45569040/table-valued-parameter-issue-with-mono-cs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!