如何使用SqlDataReader获取列的DataType和Size? [英] How to get the DataType and Size of a column using the SqlDataReader?

查看:244
本文介绍了如何使用SqlDataReader获取列的DataType和Size?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取给出的每个列的数据类型做一些验证
i已经尝试过 getSchemaTable ,但它只给我一个表的模式没有值。

i am trying to get the data type of each column given to do some verification i already tried the getSchemaTable but it only gives me the schema of a table without values.

例如,我有一个表在我的数据库和一个列名: id_declarant
我想从 id_declarant 检索值的数据类型和大小。

For example , i have a table in my database and a columnname : id_declarant. I want to retrieve the datatype and Size of a value from id_declarant.

这里是代码:

comm.Connection=new SqlConnection(connectionString);
String sql = @"
            SELECT * 
            FROM id_declarant,declarant
            WHERE (declarant.Nom_pren_RS='" + textBox1.Text + "') 
            and   (id_declarant.mat_fisc=declarant.mat_fisc)  "; 
comm.CommandText = sql;
comm.Connection.Open();
string mat_fisc;
string clé_mat_fisc;
string categorie ;
string num_etab_sec ;
string activite;
StringBuilder sb = new StringBuilder();
String Nom = textBox1.Text;
using (SqlDataReader reader = comm.ExecuteReader())
{
    while (reader.Read())
    {
        //here i want to know how to retrieve the reader[0].Type and Size to do the verification 
         mat_fisc = reader[0].ToString();
         clé_mat_fisc = reader["clé_mat_fisc"].ToString();
         categorie = reader["categorie"].ToString();
         num_etab_sec = reader["num_etab_sec"].ToString();
         activite = reader["activite"].ToString();
         sb.Append("EF" + mat_fisc + clé_mat_fisc + categorie + num_etab_sec + textBox2.Text + textBox3.Text + Nom + activite);


推荐答案

Type type = reader.GetFieldType(0);

这篇关于如何使用SqlDataReader获取列的DataType和Size?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆