A1VBCode Forums

Find in Tables


http://a1vbcode.com/vbforums/Topic30888.aspx

By shers - 11/7/2011

Hi,



I have a function that returns all the database tables with the data items in it. Is it possible to search for a value in all tables and return the table name and the datarow that contains the value?



Thanks
By AmrMohallel - 3/7/2012

Hello Shers,



You can set a list of all tables and make a loop to search in them one by one.



Here are some tips for doing so, If it doesn't help, Let me know.



- Here you can get the names of all tables in a DataTable



DataTable t = _conn.GetSchema("Tables");




- Then declare a DataTable DT and fill it with each table, one by one and search in each as follows:



-----------------------------------------------------------------------------



String X = ... ; //Where x= the compared value you're searching for.



DataTable DT = new DataTable();



// fill the datatable with data

DT = [your function to get the data]





//Now here's how you search in each record in the DataTable

for (int i = 0; i < DT.Rows.Count; i++)

{

// DT.Rows[i] Where 'i' is the vertical position of the counter in the DT record set

// .ToString() is written because DR.Rows[][] returns an object not a string.

if(DT.Rows["i"]["ColumnName"].ToString() == X)

{

found.

}

}



--------------------------------------------------------------------------------------



That was a pseudocode like. If you cant implement the previous code, please send me the solution file and Ill help you.



Regards.