Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 910
marcAntoine Avatar asked Jun 24 '13 08:06

marcAntoine


People also ask

How to retrieve data from database in c# using SqlDataReader?

To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source.

What does SqlDataReader return?

As explained earlier, the SqlDataReader returns data via a sequential stream. To read this data, you must pull data from a table row-by-row Once a row has been read, the previous row is no longer available.

Which function of DataReader is used to get the data from it?

You use the Read method of the DataReader object to obtain a row from the results of the query. You can access each column of the returned row by passing the name or ordinal reference of the column to the DataReader.


1 Answers

Type type = reader.GetFieldType(0);
like image 95
ojlovecd Avatar answered Oct 09 '22 15:10

ojlovecd