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.
- π 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
- .NET 6.0 SDK or later
- SQL Server instance (2008 or later)
- π Appropriate permissions to access target databases
-
Clone the repository:
git clone https://github.com/MohmdAliMohmd/SQL-Server-Column-Data-Type-Retriever cd sql-column-type-retriever -
Build the application:
dotnet build
-
Run the application:
dotnet run
-
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
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
-
Main Method:
- Handles user input for connection details
- Constructs secure connection strings
- Calls type retrieval method
- Displays results
-
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; }
| 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 |
- π Passwords are never stored or displayed
- πΎ Connection strings exist only in memory
- π‘οΈ Uses schema retrieval instead of dynamic SQL
- β»οΈ Proper resource disposal with
usingstatements - π« No external dependencies beyond .NET BCL
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new feature branch (
git checkout -b feature/improvement-name) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/improvement-name) - Create a new Pull Request
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.