从SQL语句检索元数据(表名称) [英] Retrieving metadata (table name) from a SQL statement

查看:252
本文介绍了从SQL语句检索元数据(表名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Visual Studio 2008。

I am using Visual Studio 2008.

我创建了一个WinForms应用程序,我想从一个SQL语句提取表名。

I have created a Winforms application, and I am trying to extract the table name from a SQL statement.

con = new SqlConnection(connString);
String queryString = "Select CUSTOMER_NAME from CUSTOMER_DETAIL";

有任何功能,这样做呢?

Is there any function to do so?

请帮我了。

推荐答案

这是很好的,你刚才提到Java的的 ResultSetMetaData的。引用该链接:

It's good that you have mentioned Java's ResultSetMetaData. Quoting that link:

公共接口ResultSetMetaData的

public interface ResultSetMetaData

这可以用来获得有关类型的信息的对象,并   列在ResultSet对象的属性。

An object that can be used to get information about the types and properties of the columns in a ResultSet object.

,您可以使用的DataColumn类。

数据类型的DataColumn 类可用于房产来获得(和设置)存储数据的类型列, AllowDBNull 可用于获取(和设置)的值,表明NULL值是否被允许在此列,等等......为了让<$的完整列表C $ C>的DataColumn 属性遵循我上面贴的链接。

DataType Property of DataColumn class can be used to get (and to set) the type of data stored in column, AllowDBNull can be used to get (and set) the value that indicates whether null values are allowed in this column, etc... To get the full list of DataColumn properties follow the link I have posted above.

样品code(获取列的数据类型):

Sample code (gets column datatype):

SqlConnection con = new SqlConnection(connString);
String queryString = "Select CUSTOMER_NAME from CUSTOMER_DETAIL";
SqlCommand cmd = new SqlCommand(queryString, con);
DataTable myTable = new DataTable();
myTable.Load(cmd.ExecuteReader());
DataColumn column = myTable.Columns[0]; // zero based index of column, alternatively use column name
string typeOfColumn = column.DataType.Name; // or column.DataType.FullName to get the fully qualified name of the System.Type

这篇关于从SQL语句检索元数据(表名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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