2

i'm trying to use the GetColumnSchema method of the SqlDataReader class. But VS tells me that SqlDataReader does not contain a definition for GetColumnSchema. Do i miss a namespace or assembly reference?

My project is using .NET Framework 4.6.1

using System;
using DocuWare.LoggingNew;
using System.Data.SqlClient;
using System.Data;
using System.Xml.Linq;
using System.Linq;
using System.IO;

private void WriteSQLQueryOutputToTextFile(string DBUser, string DBUserPassword, string sqlQuery, string databaseName, string nameOfOutputFile)
{
  StreamWriter outputFile = new StreamWriter(dWTestResult + "\\DatabaseUpgradeCheck\\" + nameOfOutputFile);           

  using (SqlConnection sqlCon = new SqlConnection("Data Source=" + GetEnvironmentVariable.MachineName + "; Initial Catalog=" + databaseName + "; User ID=" + DBUser + "; Password=" + DBUserPassword + ";"))
    {
      SqlCommand command = new SqlCommand(sqlQuery, sqlCon);
      sqlCon.Open();
      SqlDataReader reader = command.ExecuteReader();
    try
    {   
        while (reader.Read())
        {
            var columnSchema = reader.GetColumnSchema();
            string header = string.Empty;

            for (int i = 0; i < reader.FieldCount; i++)
            {
                header += $", {columnSchema[i].ColumnName}";
            }
        }
    }
    catch (Exception ex)
    {
        logger.Debug(ex, "Writing Database Output to the " + nameOfOutputFile + " file failed");
    }
    finally
    {
        reader.Close();
        outputFile.Close();
        sqlCon.Close();
    }
}

}

12
  • I'm using .NET Framework 4.6.1 as Target framework Commented Apr 5, 2019 at 8:12
  • GetColumnSchema is an extension in System.Data.Common. Are you using System.Data.Common;? Commented Apr 5, 2019 at 8:13
  • Seems like i'm using .net core version 2.1.202 Commented Apr 5, 2019 at 8:16
  • 1
    Maybe you need to upgrade to 4.7.1 Commented Apr 5, 2019 at 8:53
  • 1
    Now, i get a "Specified method is not supported" System.NotSupportedException. Classic one :D Commented Apr 5, 2019 at 9:17

2 Answers 2

2

SqlDataReader has a property GetName() which takes an index and returns the column name.

GetName() is supported in most .Net framework (from v1.1) and .Net core (from v1.0) versions.

while (reader.Read())
{
    string header = string.Empty;

    for (int i = 0; i < reader.FieldCount; i++)
    {
        header += $", {reader.GetName(i)}";
    }
}

Or you can do it using method chaining and Linq:

// returns List<string>
var columns = Enumerable.Range(0, reader.FieldCount)
                        .Select(reader.GetName)
                        .ToList();

// columns joined on ", "
var header = string.Join(", ", columns);
Sign up to request clarification or add additional context in comments.

Comments

0

From SqlDataReader.GetColumnSchema Method:

Package: System.Data.SqlClient v4.8.6

You write that you have .NET Framework 4.6.1. I have the same framework and there is no 4.8.6 framework to be targeted at the time of writing. I follow the Framework targeting overview:

enter image description here

But if I click to install other frameworks, I do not find a 4.8.6 framework so that this high version seems to be needed only for the System.Data.SqlClient. Changing my targeted framework to 4.8 did not make the program run. I did not try 4.8.1 which you could download at the time of writing:

enter image description here

I doubt that it will change something.

Still, you may try the same, keep or switch the targeted framework, and then update the System.Data.SqlClient to 4.8.6.:

enter image description here

Uninstall and install again leads to:

enter image description here

Then you might get GetColumnSchema() from the Namespace: System.Data.SqlClient, but for me, it did not work. If the package version 4.8.6 is higher than the highest framework version, I do not know which framework version is needed to make it run. Any remarks are welcome. I am just a beginner at C# and the settings for .NET in Visual Studio.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.