Data relation in ADO.net - c#

Sorry if my question is basic, when we use ADO.net to write own data access code, and we work with more than of one data table, data relation between data tables comes automatically from data base tables or we have to add data relation between data tables separately.

To understand Data Relation in Ado.Net you can read This.
when you work with more than of one data table then you can either write code yourself(using DataRelation),The DataSet contains DataTable objects and DataRelation objects. The DataRelation objects represent the relationship between two tables or you can use ERD(entity Relationship Diagram) to drag and show relationship among tables.
In my opinion database diagram is better as it reduces or exempt you do do extra code work, but manual code adds flexibility and customization.

Related

Entity Framework 6 dynamically populate data without models

I'm currently trying to read data from a table out of SQL server. The table has 10 columns and when i'm reading the base table by itself, everything works out just fine.
The trouble is, there are X number of extra property tables that may or may not go with my base table. Some databases only have the base table with 10 columns, while others have property tables containing more columns that must be joined into the base table in order to properly display the needed data. Is there a way via EF6 to load that data into a queryable source in a decoupled way?
Basically due to the fact that the extra tables are constantly in flux, I cannot rely on generating models for them and using the mapping EF provides. I do have a model for the base table as its 10 columns never change. I also have a mechanism to read the relational information in order to get the names of the property tables and columns that my program needs to display with the base table when they are available.
Any insight is greatly appreciated.
Good old-fashioned ADO.NET does a good job giving you run-time access to arbitrary query results. You can examine the column data types returned in a DataReader or after loaded into a DataTable, and access columns by name or ordinal position.
And you can interop beteween EF (for your design-time models) and ADO.NET tables that vary at runtime.

Handling multiple relational tables in data access

I have multiple relational tables in the following format.
enter image description here
I'm trying to query that data in an efficient way in .Net so that I can perform transforms on the data (array to object) and insert into DocumentDb. Essentially doing some ETL work, but because the data has to be transformed a certain way and going into DocumentDb, we are using .Net.
We are inserting into one document collection data from all relational tables, so there will be lots of
// if still in same profile record, insert more relational data for each relational table.
We are trying to avoid cartesianing(sp?) the data so that one profile doesn't have 100 records or more. We were thinking about using some of the Oracle methods to convert child records to a Json Array, but can't upgrade the Oracle system to the release that allows for that feature. Another thought was to use create an xml document, but that feels pretty wrong.
Any ideas on essentially the best practice for handling ETL work within .Net? Most of the web sites I've worked on involve only pulling from a few tables at best and a lot are 1:1 relationships.
You could use EntityFramework for this. Simply create a DBContext, and all the POCO classes that represent your tables with their relationships. Then execute the necessary query on your DataSet and you'll have all the data mapped to your objects, which then you can serialize in any way you want.

How to retrieve database relationships without EF

I have a very basic question about database programming, here's the problem:
I want to create/read/edit/etc.. data from database without using Entity Framework, and for this job I've chosen SqlFu.
I want to put the stored procedures to create, update, delete on the database and the views to get entities.
My doubt is: If I have an table Employee, that has a one-to-many relationship to Tasks table, when I create a Sql View to retrieve Employee entity, should I retrieve the data in Tasks table that is related to the employee?
If so, how to do that with a single View in SQL Server? If not, I should have different Sql Views that retrieve data from each table and bind the relationship in the application?
I'm a bit lost in this subject :S
No, you don't need. You can retrieve any data from any table/view without need to always retrieve data from any related table's.
On ORM layer it's should be implemented as lazy loading - like in EF. But not in MicroORM's like SQLFu - there you should manually do something like
employeeObject.Tasks = db.Query<Task>("select * from tasks where employeid=#0", employeeObject.Id)
When and if you actually need it.
Yes, you should retrieve data from each table and bind the relationship inside application code.

DataSet and DataTable

What is dataset and datatable?
How both of them are related to each other.
And what are the uses of it.
I also want to know how to create a dataset manually and display the data in the web page using c sharp.
As i am newbie to .net and c sharp
A data set is a collection of data tables and the relationships between them. It normally represents a set of database tables and the relationships (foreign keys) between them. See DataSet on MSDN.
A data table is usually a representation of a database table, in memory. See DataTable on MSDN.
The uses are that you do not need to go to the database every time you want to lookup a value.
The rest of your question is rather broad and not really answerable, as there are many ways to create a dataset in memory. You need to be much more specific regarding what you are trying to do.
A DataSet is an in memory representation of data,It containing one or more DataTables.
A DataTable is an in-memory representation of data, typically retrieved from a database or XML source.
As far as I remember, a DataSet is a collection of DataTables and the Relations between tables.
A DataTable is an object that represents a table of data similar to a database table.
If I were you I'd avoid the use of DataSets and DataTables as they are fairly heavyweight objects and for simple display you don't need any of the extra features that they provide.
Google for information about DataReaders and maybe DataReaders vs DataTables

Map dataset to objects

I have stored procedures that return couple of tables, and i want to map objects to the tables. Until now, i worked with type data set, and i want to stop working with them. I am looking for suggestions on how to do that, i thought about reflection, or iterate through each table in the data set and populate my object, or split the procedure to couple of procedures that each one of them return one table and iterate those tables with dataReader?
** Each object represent table in the data base.
** Each table in the procedure result contain data from specific table.
Thanks..
There are a whole bunch of options for connecting database tables with C# code, as described in this recent question.
In short - no need to do all of this manually - though if you have existing classes you need to integrate, using manual mapping might be pragmatic this time around.

Categories