Skip to content

This C# console application retrieves detailed data type information for all columns in a SQL Server table using ADO.NET. It provides a simple command-line interface to connect to your SQL Server instance and inspect table schemas.

Notifications You must be signed in to change notification settings

MohmdAliMohmd/SQL-Server-Column-Data-Type-Retriever

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—ƒοΈ SQL Server Column Data Type Retriever

Language Notice: View in Arabic (Ψ§Ω„ΨΉΨ±Ψ¨ΩŠΨ©) |

This C# console application retrieves detailed data type information for all columns in a SQL Server table using ADO.NET. It provides a simple command-line interface to connect to your SQL Server instance and inspect table schemas.

πŸš€ Features

  • πŸ” Retrieve precise SQL Server column data types
  • πŸ›  Handle special data types with parameters:
    • πŸ“ String types with length (CHAR, VARCHAR, NCHAR, NVARCHAR)
    • πŸ”’ Decimal types with precision/scale (DECIMAL, NUMERIC)
    • ⏱ Time-based types with precision (DATETIME2, TIME, DATETIMEOFFSET)
  • πŸ” Support for both Windows Authentication and SQL Server Authentication
  • πŸ”„ Case-insensitive column name handling
  • πŸ›‘οΈ Secure connection handling with proper resource disposal
  • 🚨 Comprehensive error handling

βš™οΈ Prerequisites

  • .NET 6.0 SDK or later
  • SQL Server instance (2008 or later)
  • πŸ”‘ Appropriate permissions to access target databases

πŸ–₯ How to Use

  1. Clone the repository:

    git clone https://github.com/MohmdAliMohmd/SQL-Server-Column-Data-Type-Retriever
    cd sql-column-type-retriever
  2. Build the application:

    dotnet build
  3. Run the application:

    dotnet run
  4. Follow the interactive prompts:

    SQL Server Column Data Type Retriever
    ======================================
    Enter SQL Server name: localhost\SQLEXPRESS
    Enter database name: AdventureWorks
    Enter authentication method (1 for Windows, 2 for SQL Server): 1
    Enter table name: Person.Address

πŸ“Š Example Output

Column Data Types:
------------------
AddressID: INT
AddressLine1: NVARCHAR(60)
AddressLine2: NVARCHAR(60)
City: NVARCHAR(30)
StateProvinceID: INT
PostalCode: NVARCHAR(15)
SpatialLocation: GEOMETRY
rowguid: UNIQUEIDENTIFIER
ModifiedDate: DATETIME

🧱 Code Structure

Main Components:

  1. Main Method:

    • Handles user input for connection details
    • Constructs secure connection strings
    • Calls type retrieval method
    • Displays results
  2. Core Retrieval Method:

    static Dictionary<string, string> GetColumnDataTypes(
        string connectionString, 
        string tableName)
    {
        var columnInfo = new Dictionary<string, string>(
            StringComparer.OrdinalIgnoreCase);
        
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            DataTable schemaTable = connection.GetSchema("Columns", 
                new[] { null, null, tableName, null });
            
            foreach (DataRow row in schemaTable.Rows)
            {
                // Column metadata processing
                // Special type handling logic
            }
        }
        return columnInfo;
    }

πŸ“‹ Supported Data Types

Data Type Category Examples Format
String Types CHAR, VARCHAR, NCHAR TYPE(Length)
MAX Types VARCHAR(MAX) TYPE(MAX)
Numeric Types DECIMAL, NUMERIC TYPE(Precision,Scale)
Temporal Types DATETIME2, TIME TYPE(Precision)
Other Types INT, UNIQUEIDENTIFIER TYPE

πŸ”’ Security Notes

  • πŸ”‘ Passwords are never stored or displayed
  • πŸ’Ύ Connection strings exist only in memory
  • πŸ›‘οΈ Uses schema retrieval instead of dynamic SQL
  • ♻️ Proper resource disposal with using statements
  • 🚫 No external dependencies beyond .NET BCL

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new feature branch (git checkout -b feature/improvement-name)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/improvement-name)
  5. Create a new Pull Request

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


Note: This application only retrieves schema metadata and does not access or modify table data. Always ensure you have proper permissions before accessing database schemas.

About

This C# console application retrieves detailed data type information for all columns in a SQL Server table using ADO.NET. It provides a simple command-line interface to connect to your SQL Server instance and inspect table schemas.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages