Solved

MS SQL Server getting metadata information of tables

Posted on 2006-11-21
2
260 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now