c# - 是否可以通过SqlDataReader 获取SQL 列的长度?
问题描述
使用 SqlDataReader,我可以获得列名和 SQL 数据类型,但我似乎找不到获取长度、精度或比例的方法。还有另一种方法可以获取每列的数据吗?
代码:
using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
public class StoredProcedures
{
[SqlProcedure]
public static void CLRExec(SqlString query)
{
SqlContext.Pipe.Send(DateTime.Now.ToString() + " -- " + query.ToString());
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
string sQuery = query.ToString();
// Create the record and specify the metadata for the columns.
SqlDataRecord record = new SqlDataRecord(
new SqlMetaData("col1", SqlDbType.NVarChar, 100),
new SqlMetaData("col2", SqlDbType.NVarChar, 100));
connection.Open();
SqlCommand command = new SqlCommand(sQuery, connection);
SqlDataReader reader = command.ExecuteReader();
//DataTable table = reader.GetSchemaTable();
// Mark the begining of the result-set.
SqlContext.Pipe.SendResultsStart(record);
for (int i = 0; i < reader.FieldCount; i++)
{
record.SetString(0, reader.GetName(i).ToString());
record.SetString(1, reader.GetDataTypeName(i).ToString());
// Send the row back to the client.
SqlContext.Pipe.SendResultsRow(record);
}
SqlContext.Pipe.SendResultsEnd();
}
}
}
解决方案
推荐阅读
- flutter - 在 Flutter 中为 DropdownButton 创建项目列表
- apollo - 有什么方法可以在 Apollo Studio 中格式化日期字段的渲染?
- c# - 从控制器接收模型(具有 IEnumerable 属性)时的奇怪行为
- project-reactor - Spring Cloud Stream + Kafka 动态目的地 + 错误处理
- ios - 升级到 xcode 12.5 后无法在调试中构建 ios 应用程序
- python - 抓取html,不断收到我无法解释的错误
- sql - SQLServerException:没有为“边界”的第 1 列指定列名
- dns - 如果域名指向一个IP,那么该域的cname也将指向该IP?
- amazon-web-services - AWS SSM 会话管理器未显示实例
- sql - 使用条件更新同一表中的行