I would like to generate C# class from sql queries. I can do it just for a table, but i have a header table and more than one detail tables. I' d like to generate classes for all of them and they are related with pk and fk's.
Asked
Active
Viewed 145 times
3
-
Could you elaborate the usage? – TheDude Oct 09 '18 at 10:35
-
https://gist.github.com/ernado-x/7b6f0d9f4acb95e949d5 i am using this for a table but, i want to use for related tables. i have tables related with foreign keys and i cant do this for them – Zulbahar Oct 09 '18 at 10:41
-
Have you considered using ORM tools like Dapper or Entity Framework or are you trying to avoid the extra overhead? No right or wrong answer here, just curious. – Vance McCorkle Oct 10 '18 at 18:00
1 Answers
0
I can't comment, hence the answer.
This answer isn't perfect because it's for oracle. I use bellow code to run a query and get access to the results. You can easily generate classes from this.
DataTable dataTable = new DataTable();
using (OracleConnection oracleConnection = new OracleConnection(constr))
{
oracleConnection.Open();
OracleCommand oracleCommand = new OracleCommand(query, oracleConnection);
OracleDataAdapter da = new OracleDataAdapter(oracleCommand);
//TODO: This seems to get all the data. We just want 1 row or no rows and only column info..
da.Fill(dataTable);
oracleConnection.Close();//Close just in case
}
foreach (DataColumn column in dataTable.Columns)
{
//Use these properties to generate a class
column.ColumnName;
column.DataType;
column.AllowDBNull;
column.DefaultValue;
}

fstam
- 669
- 4
- 20