last day (15 days later) » 

8:32 PM
0
Q: How to get all table names and all column and row names for each table?

bpgeckI need to query a SQL database using the C# System.Data.SqlClient dll. I know I can typically query the database using SQL query statement like SELECT * FROM TABLE_NAME. However, I don't know any of the table, column, or row names so I do not know how to properly get the information I need. I tr...

 
row.ToString() would write as DataRow; you need to try to index it to get value of a cell, like row[col.ToString()] or getting indexer and loping thrugh index count
 
@techspider makes sense but why does col.ToString() print something then?
 
hmm.... put a breakpoint and go over col.ToString() to see what property it is trying to write; it appears that you wrote code without understanding what it does!!
 
@techspider You are absolutely correct! Haha I have never done DB stuff before and this seemed like a good place to start. As for printing col.ToString() it appears this is printing a property which DataRow does not have, which is why it prints the data type instead.
 
what do you need at all? list of all tables and all column names in each table?
 
8:32 PM
@techspider Yes and how to get data from a specific row in that column
 
DataTable table = connection.GetSchema("Tables");

foreach (DataRow row in table.Rows)
{
Console.WriteLine(row[2].ToString());
}
This code returns you all Table names
refer to stackoverflow.com/questions/19392331/… to write logic to retrieve all columns for a table
write logic to get that array list inside this foreach in your code so get everything tables and columns
go from there to write queries to get rows / data you need
this is the most unconventional way of doing it
 
Haha
Alright so why the row[2]
is 2 the index of a specific table
 
0 - Database Name, 1 - Schema Name, 2 - Table Name
 
Awesome thank you so much
 
you don't need outer loop of columns
 
8:38 PM
Yeah I figured
Alright thanks I'll figure the rest out
 
cool
 
You should really just write up a short answer so I can mark it as best answer haha
You did all this work for no karma
 

last day (15 days later) »