Можно ли получить длину столбца SQL через SqlDataReader? - PullRequest
0 голосов
/ 19 июня 2020

Используя 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();
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...