我们如何从getschemaTable获取列大小和数据类型? [英] how do we get the Column Size and dataType from getschemaTable?

查看:103
本文介绍了我们如何从getschemaTable获取列大小和数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我正在尝试从数据库中的某些表中检索列NAme,大小(最大长度)和DataType,当我执行它时,以下代码期望它显示所有列类型和名称(没有找到如何引用Size,我使用了ColumnSize,但据说DataColumn不包含此方法的定义)
,但是在执行它时,它仅显示:IsColumnSetSystem.Boolean
this代码是:

I am a newbie and I am trying to retrieve the Column NAme , Size ( max legth ) and DataType from some table in my database , the following code when i execute it expecting it to display all the column types and names ( i didn't find how to refer to the Size , i used ColumnSize but it is said that DataColumn does not contain a definition for this method ) but when executing it , it only displays : IsColumnSetSystem.Boolean this is the code :

private void button1_Click(object sender, EventArgs e)
    {
        string EF = textBox1.Text;

        try{
            //SqlDataAdapter adapter = SetupDataAdapter("SELECT * FROM id_declarant");
     SqlCommand comm = new SqlCommand();
     string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=declaration;Integrated Security=True";
      comm.Connection=new SqlConnection(connectionString);
            String sql = @"SELECT * 
                  FROM id_declarant,declarant
                 WHERE (declarant.Nom_pren_RS='" + EF + "') and (id_declarant.mat_fisc=declarant.mat_fisc)  "; 
     comm.CommandText = sql;
  comm.Connection.Open();
             SqlDataReader reader = comm.ExecuteReader();
             DataTable schemaTable = reader.GetSchemaTable();
             foreach (DataRow row in schemaTable.Rows)
             {
                 foreach (DataColumn column in schemaTable.Columns)
                 {
                     System.IO.File.WriteAllText(@"C:\Users\Manuela\Documents\GL4\WriteLines.txt", column.ColumnName + column.DataType );

                 }
             }


推荐答案

您正在打印GetSchemaTable返回的数据表的列名,而不是其值,我建议您使用StringBuilder并在退出循环时编写所有内容

You are printing the column's names of the datatable returned by GetSchemaTable, not its values, also I suggest to use a StringBuilder and write everything when you exit from the loop

    StringBuilder sb = new StringBuilder();
    foreach (DataRow row in schemaTable.Rows)
    {
        foreach (DataColumn column in schemaTable.Columns)
        {
            sb.AppendLine(column.ColumnName + ":"  +row[column.ColumnName].ToString());
        }
        sb.AppendLine();
    }
    File.WriteAllText(@"C:\Users\Manuela\Documents\GL4\WriteLines.txt", sb.ToString());

这篇关于我们如何从getschemaTable获取列大小和数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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