首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.Net中的反射可以通过查看用户定义的TypeName来帮助动态构造表值参数/SqlMetaData[]对象吗?

.Net中的反射可以通过查看用户定义的TypeName来帮助动态构造表值参数/SqlMetaData[]对象吗?
EN

Stack Overflow用户
提问于 2011-08-20 03:21:10
回答 2查看 1.2K关注 0票数 5

我已经开始使用Sql Server 2k8中的表值参数进行批处理操作。我非常喜欢这个功能,感觉它是在漫长的等待之后出现的。

然而,为了从.Net代码传递TVP,有太多的工作涉及到构造SQLMetaData[],然后在循环中填充值。

如何避免维护Sql Server中的用户定义类型和.Net代码中的SQLMetaData[]对象保持同步?当我更改SQL中的类型定义时,没有简单的方法可以知道我在.Net的巨大代码中在哪里使用了该类型。

.Net反射可以通过给出用户定义类型的名称来拯救程序员来构造SQLMetadata,并通过提供对象数组来帮助填充数据。

考虑这个例子:

代码语言:javascript
运行
复制
SqlMetaData[] tvp_TradingAllocationRule = new SqlMetaData[13];
try
{
    tvp_TradingAllocationRule[0] = new SqlMetaData("ID", SqlDbType.UniqueIdentifier);
    tvp_TradingAllocationRule[1] = new SqlMetaData("Name", SqlDbType.VarChar, 255);
    tvp_TradingAllocationRule[2] = new SqlMetaData("Description", SqlDbType.VarChar, -1);
    tvp_TradingAllocationRule[3] = new SqlMetaData("Enabled", SqlDbType.Bit);
    tvp_TradingAllocationRule[4] = new SqlMetaData("Category", SqlDbType.VarChar, 255);
    tvp_TradingAllocationRule[5] = new SqlMetaData("Custom1", SqlDbType.VarChar, 255);
    tvp_TradingAllocationRule[6] = new SqlMetaData("Custom2", SqlDbType.VarChar, 255);
    tvp_TradingAllocationRule[7] = new SqlMetaData("Custom3", SqlDbType.VarChar, 255);
    tvp_TradingAllocationRule[8] = new SqlMetaData("CreatedBy", SqlDbType.VarChar, 20);
    tvp_TradingAllocationRule[9] = new SqlMetaData("CreatedTS", SqlDbType.DateTime);
    tvp_TradingAllocationRule[10] = new SqlMetaData("ModifiedBy", SqlDbType.VarChar, 20);
    tvp_TradingAllocationRule[11] = new SqlMetaData("ModifiedTS", SqlDbType.DateTime);
    tvp_TradingAllocationRule[12] = new SqlMetaData("IsFactory", SqlDbType.Bit);
}
catch (Exception ex)
{
    throw new Exception("Error Defining the tvp_TradingActionCondition in .Net" + ex.Message);
}

foreach (TradingRuleMetadata ruleMetadata in updatedRules)
{
    SqlDataRecord tradingAllocationRule = new SqlDataRecord(tvp_TradingAllocationRule);
    try
    {
        tradingAllocationRule.SetGuid(0, ruleMetadata.ID);
        tradingAllocationRule.SetString(1, ruleMetadata.Name);
        tradingAllocationRule.SetString(2, ruleMetadata.Description);
        tradingAllocationRule.SetBoolean(3, ruleMetadata.Enabled);
        tradingAllocationRule.SetString(4, ruleMetadata.Category);
        tradingAllocationRule.SetString(5, ruleMetadata.Custom1);
        tradingAllocationRule.SetString(6, ruleMetadata.Custom2);
        tradingAllocationRule.SetString(7, ruleMetadata.Custom3);
        tradingAllocationRule.SetString(8, ruleMetadata.CreatedBy);
        tradingAllocationRule.SetDateTime(9, ruleMetadata.CreatedDate);
        tradingAllocationRule.SetString(10, ruleMetadata.ModifiedBy);
        tradingAllocationRule.SetDateTime(11, ruleMetadata.ModifiedDate);
        tradingAllocationRule.SetBoolean(12, ruleMetadata.IsFactory);
        tvp_TradingAllocationRuleRecords.Add(tradingAllocationRule);
    }
    catch (Exception ex)
    {

    }
}

现在,如果您的表有100列,想象一下您的代码。

EN

Stack Overflow用户

发布于 2011-08-20 08:31:48

问题中没有足够的内容来提供代码示例,但是对于这样的事情,我会编写一个单独的.NET可执行文件来读取SQL元数据并为每个UDT生成帮助器类(看起来与您的示例非常相似)。代码生成的优点是它在运行时速度更快,更重要的是,您可以像手写代码一样阅读和逐步执行源代码。这也不是特别困难--特别是现在有了partial关键字。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7126372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档