Solved

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

Posted on 2009-05-11
6
1,172 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now