0

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.

Ranjan Kumar
  • 497
  • 2
  • 8
  • 24

1 Answers1

0

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);
Ranjan Kumar
  • 497
  • 2
  • 8
  • 24