获取列的大小以及列的名称和数据类型[英] Get columns size along with column name and datatype

本文是小编为大家收集整理的关于获取列的大小以及列的名称和数据类型的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从 SQL Server 数据库中获取所选表和数据库的列、数据类型和列大小.

到目前为止,我已成功获取列名和数据类型,但我也想获取大小.

示例:考虑我在 SQL Server 数据库表中有 2 列,如下所示:

1) Name nvarchar(30)
3) Salary decimal(6,2)

现在我可以使用 nvarchar 和 salary 数据类型获取姓名和薪水,但大小不会到来.

代码:

String[] columnRestrictions = new String[4];
columnRestrictions[0] = 'MyDb';
columnRestrictions[1] = 'dbo';
columnRestrictions[2] = 'Employee';
using (SqlConnection con = new SqlConnection("MyConnectionString"))
{
    con.Open();
    var columns = con.GetSchema("Columns", columnRestrictions).AsEnumerable()
        .Select(c => new (c[3].ToString(), c.Field<string>("DATA_TYPE"))).ToList();

我得到的结果如下:

Name nvarchar
Salary decimal

预期结果:

1) Name nvarchar(30)
3) Salary decimal(6,2)

源代码:参考

是否有可能得到以上结果?

推荐答案

您可以将 CHARACTER_OCTET_LENGTH 用于 char/varchar 和 Numeric_precision + Numeric_Scale> 用于数字类型.

本文地址:https://www.itbaoku.cn/post/1556855.html

问题描述

I am trying to fetch columns, datatypes and size of columns of selected table and database from SQL Server database.

So far I am successful in fetching column names and datatypes but I want to fetch size also.

Example: Consider I have 2 columns in SQL Server database tables like below:

1) Name nvarchar(30)
3) Salary decimal(6,2)

Now I am able to fetch Name and salary with nvarchar and salary datatypes but size is not coming.

Code:

String[] columnRestrictions = new String[4];
columnRestrictions[0] = 'MyDb';
columnRestrictions[1] = 'dbo';
columnRestrictions[2] = 'Employee';
using (SqlConnection con = new SqlConnection("MyConnectionString"))
{
    con.Open();
    var columns = con.GetSchema("Columns", columnRestrictions).AsEnumerable()
        .Select(c => new (c[3].ToString(), c.Field<string>("DATA_TYPE"))).ToList();

Result I am getting is like below:

Name nvarchar
Salary decimal

Expected result:

1) Name nvarchar(30)
3) Salary decimal(6,2)

Source code: Reference

Is it possible to get above result?

推荐答案

you can use CHARACTER_OCTET_LENGTH for char/varchar and the Numeric_precision + Numeric_Scale for numeric types.