BDPL
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Hi, thanx.
But i am not gettting datatype name. I got just datatype heading, in that no available names
plz..
But i am not gettting datatype name. I got just datatype heading, in that no available names
plz..
ASKER
Hey,
Sorry
I got the exact output. Your qery is nice and simple.
thanx.
Sorry
I got the exact output. Your qery is nice and simple.
thanx.
you can get more information about column as well, have a look at by SELECT *
using System.Data;
using System.Data.SqlClient;
public class MainClass
{
public static void Main()
{
SqlConnection thisConnection = new SqlConnection("server=(loc
//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("Connect
// Execute Query
SqlDataReader thisReader = thisCommand.ExecuteReader(
// Get column data types
Console.WriteLine("Data types: {0} | {1}", thisReader.GetDataTypeName
//Close DataReader
thisReader.Close();
}
catch (SqlException ex) {
// Display error
Console.WriteLine("Error: " + ex.ToString());
}
finally {
// Close Connection
thisConnection.Close();
Console.WriteLine("Connect
}
}
}