Advertisements
RSS

Getting the Table Name of a field in a View (SQL2000, C#)

10 Oct

So I have a View that join different tables, I wanted to get the Table name of a Field in the View. So I created a Function [GetTableName] with the following Inputs

 

         View Name

         Connection String of the View.

         The required Field Name

 

This is the function

 

public string GetTableName (string ViewName, string ConnString, string FieldName)

        {

            SqlConnection MyConnection;

            string toReturn = ViewName;

 

            MyConnection = new SqlConnection(ConnString);

            MyConnection.Open();

 

            DataTable MyTable = MyConnection.GetSchema(“Views”);

            foreach (DataRow dRow in MyTable.Rows)

            {

                object[] sqlArray = dRow.ItemArray;

                if (sqlArray.GetValue(2).ToString() == ViewName)

                {

                    DataTable MyVTable = MyConnection.GetSchema(“ViewColumns”);

                    foreach (DataRow dVRow in MyVTable.Rows)

                    {

                        object[] sqlVArray = dVRow.ItemArray;

                        if (sqlVArray.GetValue(2).ToString() == ViewName)

                        {

                            if (sqlVArray.GetValue(6).ToString() == FieldName)

                            {

                                toReturn = sqlVArray[5].ToString();

                            }

                        }

 

 

                    }

 

                }

            }

           

            return toReturn;

        }

 

 

 

Advertisements
 
Leave a comment

Posted by on October 10, 2008 in General, Photography

 

Tags: , , , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: