LINQ table enumeration with column enumeration

How do you get a list of all tables and use that list to list the columns? I found posts that describe one or the other, but not both.

My net result: I want to create a static class that contains the names of all the columns in each table, so that I can, for example, do:

comboBoxFoo.DisplayMember = SomeNamespace.SomeTable.SomeDisplayColumnName;
comboBoxFoo.ValueMember = SomeNamespace.SomeTable.SomeIDColumnName;
comboBoxFoo.DataSource = dingo;

      

I am currently using the this method , which, although it works, means I have to manually create my tables in the list.

I have a separate command line project that generates the SomeNameSpace.SomeTable class manually, and I add the generated class file to the project.

Ideally, if I could go through the foreach of tables and do something like this:

foreach(var table in someTableNumerationMethodForAGivenContext())
{
    var columnList = databaseContext.ColumnNames<someTable>();
    foreach(var columnData in columnList)
    {
        DoJazz(columnData.Name);
    }
}

      

Is there a better way to do this other than manually by doing databaseContext.ColumnNames ()?

Edit 1: We are using LinqToSQL. Moving to ADO.NET is also an option in the spreadsheet, but for now we don't need a real need.

Edit 2: I know L2S does data binding, but what I need is getting the list of column names as rows from the table. L2S doesn't offer this, or it's not obvious on my side. I would like to do something like: SomeTable.SomeColumn.ToString () or something else. SubSonic has it.

Finale: Thank you all. all very good answers and lead me to the answer. You guys rock!

0


a source to share


4 answers


Nazadus,

Is this what you are looking for?



LINQ to SQL Trick: Get all names in table [and column]: http://blogs.msdn.com/jomo_fisher/archive/2007/07/30/linq-to-sql-trick-get-all-table-names .aspx

+1


a source


What you are describing is essentially an ORM



Linq to SQL is an ORM that will create prototypes of C # classes for you that contain the information you describe. It perfectly supports the kind of data binding you illustrated.

0


a source


I think you are looking for this .

DataContext.Mapping.GetTable(yourTable).RowType.DataMemebers()

      

0


a source


I can think of two ways to do this.

A) Use SMO to get all tables / columns in the database. You need to specify:

Microsoft.SqlServer.ConnectionInfo

Microsoft.SqlServer.Management.Sdk.Sfc

Microsoft.SqlServer.Smo

Then you can do something like this:

        ServerConnection connection = new ServerConnection(".");
        Server server = new Server(connection);
        Database db = server.Databases["Northwind"];
        foreach (Table table in db.Tables)
        {
            foreach (Column column in table.Columns)
            {
                Console.WriteLine("Table: {0}, Column: {1}",table.Name,column.Name);
            }
        }
    }

      

B) Use reflection to mirror your assembly with dataclasses generated by Linq to Sql. Anything that has a Table attribute is a table, and anything that has a Column attribute is a column. Let me know if you need a sample ...

0


a source







All Articles