How to get the DataType and Size of a column using the SqlDataReader?

元气小坏坏 提交于 2019-12-12 11:43:08

问题


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.

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.

Here is The code :

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);

回答1:


Type type = reader.GetFieldType(0);



回答2:


Please use function GetTableSchema .

SqlDataReader reader= command.ExecuteReader();

using (var schemaTable = reader.GetSchemaTable())
    {
        foreach (DataRow row in schemaTable.Rows)
        {
            string ColumnName= row.Field<string>("ColumnName");
            string DataTypeName= row.Field<string>("DataTypeName");
            short NumericPrecision= row.Field<short>("NumericPrecision");
            short NumericScale= row.Field<short>("NumericScale");
            int ColumnSize= row.Field<int>("ColumnSize");
            Console.WriteLine("Column: {0} Type: {1} Precision: {2} Scale: {3} ColumnSize {4}",      
            ColumnName, DataTypeName, NumericPrecision, scale,ColumnSize);
        }
    }

Using Table schema , you can get all column related property using c# .

Thanks .




回答3:


You can use GetDataTypeName() function to get the data type of the field

   String dataType = reader.GetDataTypeName(FIELD_INDEX);



回答4:


public string ReadString(IDataReader reader, string columnName) {

string myString = "";

var index = reader.GetOrdinal(columnName);

var fieldType = reader.GetFieldType(index);

if (fieldType.FullName.Contains("Guid"))
{
myString = reader.IsDBNull(index) ? "" : reader.GetGuid(index).ToString();
}
else
{
myString = reader.IsDBNull(index) ? "" : reader.GetString(index);
}
return myString;
}


来源:https://stackoverflow.com/questions/17271143/how-to-get-the-datatype-and-size-of-a-column-using-the-sqldatareader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!