Solved

MS SQL Server getting metadata information of tables

Posted on 2006-11-21
2
263 Views
Last Modified: 2008-01-09
Hi,

Can someone point me to an example of the following:

I want to create a simple page which will query a table 'mytable' like: select id, name, address from mytable
and create the a table of html elements (no data grid) in a way that for id field textfield is displayed, for name textfields is displayed, for address textarea is displayed.

What I want to know is that how can you get the MetaData information so that i can check that if a field size is greater that 10 then display textarea else textfield, secondly if the field type is int then add a javascript validation to it (that is not a problem to add a js valid.) third i want to know the name of the column...

any help would be appreciated,

thanks.
0
Comment
Question by:glottis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
ID: 17995101
You can get schema information with a data reader's GetSchemaTable method.

How To Retrieve Column Schema by Using the DataReader GetSchemaTable Method and Visual C# .NET
http://support.microsoft.com/kb/310107

C# example:

OleDbConnection cn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
DataTable schemaTable;
OleDbDataReader myReader;
                  
//Open a connection to the SQL Server Northwind database.
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=login;
                       Password=password;Initial Catalog=Northwind";
cn.Open();

//Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM Employees";
myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();

//For each field in the table...
foreach (DataRow myField in schemaTable.Rows){
    //For each property of the field...
    foreach (DataColumn myProperty in schemaTable.Columns) {
      //Display the field name and value.
      Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
    }
    Console.WriteLine();

    //Pause.
    Console.ReadLine();
}

//Always close the DataReader and connection.
myReader.Close();
cn.Close();

Bob
0
 
LVL 2

Author Comment

by:glottis
ID: 18001119
Bob,

Thanks for that. I will look at this and try to implement it in a web based application, and if successfull will award you the points.

Thanks again,

Imran.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question