Solved

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

Posted on 2009-05-11
6
1,180 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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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