Programmatic output of MS SQL View columns including alias in .net / C #?

How do I get the column name (including the alias) of a table associated with a View in C #?

+1


a source to share


3 answers


You can call the following code from C # in several ways. I'll let you know which one to use;)



Select c. * From sys.all_views v join sys.all_columns c to c.object_id = v.object_id Where v.name = 'MyView'

+3


a source


You can simply query it like a normal table. Alias ​​names must appear as regular column names.

SqlConnection con = new SqlConnection ("MyConnectionString");

DataTable dt = new DataTable ();



SqlDataAdapter da = new SqlDataAdapter ("Select * FROM vw_MyView", con);

da.fill (DS);

0


a source


How about GetSchema on a connection i.e.

Dim conn As New SqlConnection(
"Data Source=YOURMACHINE;initial catalog=YOURDB;Integrated security=true")

conn.Open()

Dim dt As New DataTable

Dim viewName(3) As String
viewName(0) = Nothing
viewName(1) = Nothing
viewName(2) = "YOURVIEWNAME"


dt = conn.GetSchema(SqlClientMetaDataCollectionNames.Views, viewName)

      

ie http://msdn.microsoft.com/en-us/library/ms136367(VS.80).aspx

0


a source







All Articles