Solved

To get Column datatype from sqlserver table in C#-dotnet

Posted on 2009-05-11
6
1,189 Views
Last Modified: 2013-12-17
Hi,
I have a table with 3 columns.

column   datatype
--------    -----------
x               int                with primary key
y               char(10)        
z              decimal(10,4)

I want to write a query, to get the particular column datatype  from this table, in C#-dotnet winforms application.

Plz, help on this
thanx.
0
Comment
Question by:BDPL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 250 total points
ID: 24353092
select data_type from information_schema.columns where table_name='yourTable' and column_name='yourColumn'
0
 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 24353097
using System;
using System.Data;
using System.Data.SqlClient;


public class MainClass
{
    public static void Main()
    {
        SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;" + "integrated security=sspi;database=MyDatabase");
       
        //Sql Query 1
        string sql = "SELECT x,y,z FROM tablename; ";
       
        //Create Command object
        SqlCommand thisCommand = new SqlCommand(sql, thisConnection);
       
        try {
            // Open Connection
            thisConnection.Open();
            Console.WriteLine("Connection Opened");
           
            // Execute Query
            SqlDataReader thisReader = thisCommand.ExecuteReader();
           
            // Get column data types
            Console.WriteLine("Data types: {0} | {1}", thisReader.GetDataTypeName(0).PadLeft(13), thisReader.GetDataTypeName(1));
           
           
            //Close DataReader
               
            thisReader.Close();
        }
        catch (SqlException ex) {
            // Display error
            Console.WriteLine("Error: " + ex.ToString());
        }
        finally {
            // Close Connection
            thisConnection.Close();
               
            Console.WriteLine("Connection Closed");
        }
    }
}
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24353118
well you can go for simple C# code also but my query is very simple and small, will give you all column name and datatype if you will just pass table name.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:BDPL
ID: 24353147
Hi, thanx.
But i am not gettting datatype name. I got just datatype heading, in that no available names

plz..
0
 

Author Comment

by:BDPL
ID: 24353157
Hey,

Sorry

I got the exact output. Your qery is nice and simple.

thanx.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24353179
you can get more information about column as well, have a look at by SELECT *
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question