DataReader 获取类型规范 SQL 服务器
DataReader get type specification SQL Server
我有一个小程序可以"download"数据库表到Excel。
我想将列类型添加到第二行,我尝试了以下功能。它工作正常,但 GetDataTypeName(i)
returns 仅 int, nvarchar
但我需要像这样的完整类型规范
nvarchar(255), decimal(19, 8)
是否有其他函数可以从数据库中获取它?
SqlDataReader dataReader = command.ExecuteReader();
// adds the names and the types if the table has no values
if (!dataReader.HasRows || !withValues)
{
for (int i = 0; i < dataReader.FieldCount; i++)
{
names.Add(dataReader.GetName(i));
types.Add(dataReader.GetDataTypeName(i));
}
}
此类信息可通过调用 GetSchemaTable 获得。它 returns 一个数据表,其中查询返回的每一列都有一行。 table 的每一列描述了元数据提取的与查询字段相关的特定信息
例如
SqlDataReader dataReader = command.ExecuteReader();
if (!dataReader.HasRows || !withValues)
{
DataTable dt = dataReader.GetSchemaTable();
foreach(DataRow row in dt.Rows)
{
Console.WriteLine("ColumnName: " + row.Field<string>("ColumnName"));
Console.WriteLine("NET Type: " + row.Field<string>("DataTypeName"));
Console.WriteLine("Size: " + row.Field<int>("ColumnSize"));
}
}
GetSchemaTablereturns很多关于你的信息table/query,但是很多这些字段都设置为null。我不确定这是提供者的限制还是它们为空,因为在调用的上下文中,它们没有任何意义。在任何情况下都使用防御性编程来访问这些值(if !(value == DBNull.Value)
请使用 TableSchema 方法获取列的所有详细信息。
SqlDataReader reader= command.ExecuteReader();
using (var schemaTable = reader.GetSchemaTable())
{
foreach (DataRow row in schemaTable.Rows)
{
string ColumnName= row.Field<string>("ColumnName");
string DataTypeName= row.Field<string>("DataTypeName");
short NumericPrecision= row.Field<short>("NumericPrecision");
short NumericScale= row.Field<short>("NumericScale");
int ColumnSize= row.Field<int>("ColumnSize");
Console.WriteLine("Column: {0} Type: {1} Precision: {2} Scale: {3} ColumnSize {4}",
ColumnName, DataTypeName, NumericPrecision, scale,ColumnSize);
}
}
谢谢。
我有一个小程序可以"download"数据库表到Excel。
我想将列类型添加到第二行,我尝试了以下功能。它工作正常,但 GetDataTypeName(i)
returns 仅 int, nvarchar
但我需要像这样的完整类型规范
nvarchar(255), decimal(19, 8)
是否有其他函数可以从数据库中获取它?
SqlDataReader dataReader = command.ExecuteReader();
// adds the names and the types if the table has no values
if (!dataReader.HasRows || !withValues)
{
for (int i = 0; i < dataReader.FieldCount; i++)
{
names.Add(dataReader.GetName(i));
types.Add(dataReader.GetDataTypeName(i));
}
}
此类信息可通过调用 GetSchemaTable 获得。它 returns 一个数据表,其中查询返回的每一列都有一行。 table 的每一列描述了元数据提取的与查询字段相关的特定信息
例如
SqlDataReader dataReader = command.ExecuteReader();
if (!dataReader.HasRows || !withValues)
{
DataTable dt = dataReader.GetSchemaTable();
foreach(DataRow row in dt.Rows)
{
Console.WriteLine("ColumnName: " + row.Field<string>("ColumnName"));
Console.WriteLine("NET Type: " + row.Field<string>("DataTypeName"));
Console.WriteLine("Size: " + row.Field<int>("ColumnSize"));
}
}
GetSchemaTablereturns很多关于你的信息table/query,但是很多这些字段都设置为null。我不确定这是提供者的限制还是它们为空,因为在调用的上下文中,它们没有任何意义。在任何情况下都使用防御性编程来访问这些值(if !(value == DBNull.Value)
请使用 TableSchema 方法获取列的所有详细信息。
SqlDataReader reader= command.ExecuteReader();
using (var schemaTable = reader.GetSchemaTable())
{
foreach (DataRow row in schemaTable.Rows)
{
string ColumnName= row.Field<string>("ColumnName");
string DataTypeName= row.Field<string>("DataTypeName");
short NumericPrecision= row.Field<short>("NumericPrecision");
short NumericScale= row.Field<short>("NumericScale");
int ColumnSize= row.Field<int>("ColumnSize");
Console.WriteLine("Column: {0} Type: {1} Precision: {2} Scale: {3} ColumnSize {4}",
ColumnName, DataTypeName, NumericPrecision, scale,ColumnSize);
}
}
谢谢。