Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1199
  • Last Modified:

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

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
BDPL
Asked:
BDPL
  • 3
  • 2
1 Solution
 
RiteshShahCommented:
select data_type from information_schema.columns where table_name='yourTable' and column_name='yourColumn'
0
 
Chandan_GowdaCommented:
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
 
RiteshShahCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BDPLAuthor Commented:
Hi, thanx.
But i am not gettting datatype name. I got just datatype heading, in that no available names

plz..
0
 
BDPLAuthor Commented:
Hey,

Sorry

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

thanx.
0
 
RiteshShahCommented:
you can get more information about column as well, have a look at by SELECT *
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now