【问题标题】:How to get the DataType and Size of a column using the SqlDataReader?如何使用 SqlDataReader 获取列的 DataType 和 Size?
【发布时间】:2013-06-24 08:33:53
【问题描述】:

我正在尝试获取给定的每一列的数据类型以进行一些验证 我已经尝试过getSchemaTable,但它只给了我一个没有值的表的架构。

例如,我的数据库中有一个表和一个列名:id_declarant。 我想从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);

【问题讨论】:

    标签: c# .net database getschematable


    【解决方案1】:
    Type type = reader.GetFieldType(0);
    

    【讨论】:

    • 你能详细说明你的答案吗?
    • 我试过这个:string Type= reader["id_declarant"].GetFieldType(0).FullName;但它显示 System.string
    • 它的类型 type = reader.GetFieldType(0);而不是 string Type= reader["id_declarant"].GetFieldType(0).FullName;顺便说一句,id_declarant coumn 的数据类型是什么
    • 是的 reader.GetFieldType(0) 显示 reader 的第 0 行中每个值的 DataType ?? BTW id_declarant 是 nvarchar(7)
    • reader.GetFieldType(0) 显示第一列的 .net 数据类型。所以数据库中id_declarant的数据类型对应C#中的System.String类型
    【解决方案2】:

    请使用函数 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, NumericScale, ColumnSize);
            }
        }
    

    使用 Table 架构,您可以使用 c# 获取所有与列相关的属性。

    谢谢。

    【讨论】:

      【解决方案3】:

      您可以使用 GetDataTypeName() 函数来获取字段的数据类型

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-02-24
          • 2011-03-07
          • 2016-09-17
          • 2010-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多