获取表的列名并将其存储在字符串或var c#asp.net中 [英] Get the column names of a table and store them in a string or var c# asp.net

查看:50
本文介绍了获取表的列名并将其存储在字符串或var c#asp.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获取数据库表的列并将其存储在字符串或字符串数​​组中.我有以下代码,但我认为它不起作用.我正在使用asp.net中提供的默认表.我已经可以毫无问题地写入此表,但是我无法弄清楚如何从中选择并保存检索到的值.这就是我的代码后面的内容

I was wondering how I could get the columns of a database table and store each of them in a string or string array. I have the following code but I believe it does not work. I'm using the default table that is given in asp.net. I've been able to write to this table no problem but I cannot figure out how to select from it and save the values retrieved. here is what I have in my code behind

    string connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    SqlConnection conn = null;
    conn = new SqlConnection(connection);
    conn.Open();
    using (SqlCommand cmd = new SqlCommand())
    {

        string query = String.Format("SELECT column_name FROM USER_TAB_COLUMN WHERE table_name = 'TestTable'");
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = query;
        columns.Text = cmd.ExecuteNonQuery().ToString();
        conn.Close();
    }

错误是无效的对象名称'USER_TAB_COLUMN'.我试着删除它,并使用从TestTable中选择SELECT column_name" ,然后它抱怨 column_name .列是一个文本框.

the error is Invalid object name 'USER_TAB_COLUMN'. I've tried removing this and using "SELECT column_name FROM TestTable" then it complains about column_name. Columns is a text box by the way.

推荐答案

您可以使用

You can use DbDateReader.GetSchemaTable.

DataTable schema = null;
using (var con = new MySql.Data.MySqlClient.MySqlConnection(connection))
{
    using (var schemaCommand = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM TestTable", con))
    {
        con.Open();
        using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
        {
            schema = reader.GetSchemaTable();
        }
    }
}
foreach (DataRow col in schema.Rows)
{
    Console.WriteLine("ColumnName={0}", col.Field<String>("ColumnName"));
}

列名在每一行的第一列中.

The column name is in the first column of every row.

我正在尝试您的方法,但MySql.Data.MySqlClient.MySqlConnection(connection))抛出类型或找不到命名空间

I am trying your method but MySql.Data.MySqlClient.MySqlConnection(connection)) is throwing type or namespace could not be found

我可能发誓我曾经见过 MySqlCommand MySqlConnection .因此,您将SQL Server用作rdbms吗?

I could have sworn that i have seen MySqlCommand and MySqlConnection. So you are using SQL-Server as rdbms instead?

using (var con = new SqlConnection(connection))
{
    using (var schemaCommand = new SqlCommand("SELECT * FROM TestTable;", con))
    {
        con.Open();
        using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
        {
            schema = reader.GetSchemaTable();
        }
    }
}
// DataTable part is the same

这篇关于获取表的列名并将其存储在字符串或var c#asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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