c# - 如何在 C# 中以结构化方式访问 SqlDbType 的 Sql 参数的列和数据?
问题描述
在 C# 中,对于表值参数,我添加了一个 SqlParameter,其中“SqlDbType”作为“Structured”,“Value”作为 C# DataTable。我想稍后在我的代码中提取这些数据。
- 我想验证 SqlDbType/DbType 是否为“结构化”。
- 如果是,并且“Value”是“DataTable”,我想获取其列的 columnNames 和 DataRows 中的数据。
下面是 SqlParameter 的代码。
DataTable memoIdDt = new DataTable();
SqlParameter param = new SqlParameter ("memos", SqlDbType.Structured) { Value = memoIdDt, TypeName = "Table_Type_In_DB" };
后来我想做类似下面的事情(这不是确切的代码)。
//I am not able to use param.SqlDbType. I can use the param.DbType property.
//But it returns Object. So, not able to get the if clause right.
If(param.DbType == SqlDbType.Structued)
{
//foreach column in param.Value.Columns, print columnNames
//foreach DataRow in param.Value, print the array
}
如果您知道如何实现这一点,请提供帮助。
解决方案
我认为您可以简单地param.Value
转换回 a DataTable
:
if (param.SqlDbType == SqlDbType.Structured)
{
var table = param.Value as DataTable;
foreach (DataColumn column in table.Columns) Console.WriteLine(column.ColumnName);
foreach (DataRow row in table.Rows) Console.WriteLine(row.ItemArray.Length);
}
推荐阅读
- sql - SQL Count 基于另一列的列数
- visual-studio-code - JSON Schema - 数组/列表验证可以与 anyOf 结合使用吗?
- sql-server - 如何在 SQL Server 的表中存储存储过程
- javascript - ExpressJS Node.js Angular 错误:获取 OAuth2 API 授权令牌时出现 getaddrinfo ENOTFOUND
- angular - 无法使用键盘输入日期
- sql - PostgreSQL 命令在我尝试在 psql shell 中再次运行它们之前不起作用
- unity3d - 让绳子“摇摆”
- javascript - 找不到页面和默认主页总是重定向到主页 - React Router
- jquery - 如何在 ASP.NET Razor 视图中使用 jQuery 将模型发送回 ASP.NET 控制器
- ios - iOS:QLPreviewController 不预览 Pages 或 Word 文档