Table Valued Parameter issue with MONO cs(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 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 的表值参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MONO cs 的表值参数问题
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
