working with multiple tables - c#

I have a c# project where I am using a MS access database, and I am trying to put some items from two (related) tables, in the same listView.
This is the code that I have:
Where it sais "none" is the place where I want to put an item from the other table, but i don't know how. Since they have a relationship I thought that was possible to call items from one table or another.
Please note that I am fairly new to c# and working with databases.

You should join the two tables together to get the unified result.
You could do something like this:
select c.*
, s.otherFieldYouNeed
from ClientsT c
join SomeOtherTable s
on c.someId = s.someId

since the two table are related you can use a join query to get the desired result:
select FirstName,LastName,'none',CellPhone,ClientID from Clients inner join mySecondTable
on Clients.mysharedField=mySecondTable.mysharedField

select FirstName,LastName,(select none from SecondTable where Clients.Field= SecondTable.Field),CellPhone,ClientID from Clients

Try altering your query to include this information. Something like:
Select FirstName, LastName, ColumnFromOtherTable, CellPhone, ClientsT.ClientID
from ClientsT, OtherTable where ClientsT.ClientID=OtherTable.ClientID
Of course, in order to do this, you have to have a corresponding ID field in both tables.

Related

How to make an algorithm to get all column names from across tables that relations with their primary key in SQL Server and C#?

I have 3 tables (for example 3, but in real over than 30 tables with this conditions) in my SQL Server database: post, user, person.
post: (post_id, post_text, user_id)
user: (user_id, user_name, person_id)
person: (person_id, person_phone, person_email)
Now, in C#, I want an algorithm that creates a query that get result like this:
post.post_id, post.post_text, post.user_id, user.user_id, user.user_name, user.person_id, person.person_id, person.person_email
and I use this method for fill a SqlDataReader in C# for reading and accessing all column values from these records.
I know that the common way to get that result directly and manually using of 'Join' statement, but it is waste time if tables count is very much. So, I want an algorithm that generates this query for manipulate in C# programming.
Thanks a lot.
To query all column names from your tables, you can use:
SELECT obj.Name + '.' + col.Name AS name
FROM sys.columns col
INNER JOIN sys.objects obj
ON obj.object_id = col.object_id
WHERE obj.Name IN ('post', 'user', 'person')
ORDER BY name
Then, for how to call this from C# using SqlDataReader, you can use this documentation: https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
select post1.post_id, post1.post_text, post1.user_id, user1.user_id, user1.user_name, user1.person_id, person1.person_id, person1.person_email from post post1 inner join user user1 on user1.user_id=post1.user_id inner join person person1 on person1.person_id=user1.person_id

How can I display only specific data in my gridview considering other database tables field in my database

I have a database table tbl_Employee which stores employee_code as primary and another database table tbl_Salary which has a fields employee_code and ammount. Now, my question is, how can I display on my datagriview those employees that has their salary set?
At first, you must explain your question a bit more. For example, you have pointed out only one column name and two table names. What we want to retrieve from tbl_salary? And you must write your code which causes problem or on this situation, you must show examples for your tables. That is the reason that you have a downvote already.
Now, for your question, use SQL command like this;
SELECT *.tbl_Employee, *.tbl_Salary
FROM tbl_Employee INNER JOIN tbl_Salary
ON tbl_Employee.employee_code = tbl_Salary.employee_code
I am not sure what exactly you might be looking for. Since I dont have the REP to comment I assume You might be looking for a query to get the data where salary of the employees has been set and thus you can get the data like this
USING JOIN
SELECT te.* from tbl_Employee te
JOIN tbl_Salary ts ON
te.employee_code = ts.employee_code
Using WHERE
SELECT te.* from tbl_Employee te, tbl_Salary ts
WHERE te.employee_code = ts.employee_code

How to make a query on multiple(joined) Tables using Azure Mobile Services with windows phone 8?

I have two tables, they are indexed in the azure database manager. So i have the foreign key in the second table.
My tables are for example
OrderTable (OrderId,OrderDate,CustomerId) /CustomerId is my foreign key
Customer Table( CustomerId,CustomerName,....)
So i just want a query like this:
Select *
From OrderTable o1,CustomerTable c1
Where c1.CustomerId=o1.CustomerId
I used the microsoft sample TodoItems, and i already can make querys on one table like this:
items = await todoTable
.Where(todoItem => todoItem.Date >= DateTime.Now)
.ToCollectionAsync();
.ToListAsync();
So in my app i got the two table, is there any option to query the joined tables like the one above ?
You can perform joins in LINQ, but in your situation, it's probably easier to create a view that does the join and then select from that using LINQ.
Also, you should avoid using the older join syntax as you have - it will stop being supported at some point - and use the INNER JOIN clause, i.e.
SELECT * FROM OrderTable o1 INNER JOIN CustomerTable c1
ON c1.CustomerId = o1.CustomerId
Create a view like Rikalous pointed out. You can do this by clicking on Sql Databases on your windows azure portal. Select your server and then click on the "Manage URL" located on the Dashboard page at the bottom right.
Once you login, click on "New Query" and then just type in the sql code to create a view.
CREATE mySchema.myView AS
SELECT * FROM Table t1 INNER JOIN OtherTable t2 ON t1.a=t2.b
Once your view is created, go back to your Windows Azure Portal. Go to your Mobile Service and create a new table. Create the table with the name of your view, the system will detect the view and present it to you. You will see that no default columns are present nor any data will show up. But you will be able to query it as any other table, plus you can also modify its insert/update/read scripts.
*Important: double check that your view is created on the correct schema. Also double check after adding the table on Mobile Services that no table was created on the server.

How to know which columns correspond between tables and a view

I've got a view in SQL Server, and would like to join this view with other tables, through my C# application. To accomplish this, I would need to find out which columns the respective view's fields correspond with, in the underlying table. For example, I could have a view like so:
CREATE VIEW [View A]
AS
SELECT Children.Child_ID, Social_Workers.Social_ID
FROM Children
INNER JOIN Social_Workers
ON Children.Social_ID = Social_Workers.Social_ID
I may want to join a table to the above view. To accomplish this, my C# application must somehow know which are the required foreign key and primary key fields within the relationship, thus generating SQL code like so:
SELECT [View A].Child_ID,
Sponsors.User_ID
FROM [View A]
INNER JOIN Sponsors
ON [View A].Child_ID = Sponsors.Child_ID
I have found a way to retrieve the underlying tables within the view, however I am unsure of how to approach the rest of the problem.
I think the following might help, specifically the referenced_entity_name and referenced_minor_name columns.
select * from sys.dm_sql_referenced_entities( 'dbo.ViewA', 'object' ) ;
This DMV was added in SQL Server 2008 so if you have an earlier version, no help I'm sorry.
If i right understand your requirements, you can try to use queries from Find the real column name of an alias used in a view?

Querying a join table using LINQ

Okay this could very likely be a silly question. I am using Entity Framework Code First. I have two classes, User and Event, that have a Many-To-Many relationship. When EF generates my database tables, it creates a join table, which I call Users_Events. This table has two columns, User_ID and Event_ID. Everything is fine so far.
I want to pull an Event from my database and serialize it to JSON. This also works perfectly except I cannot pull an Event's Users because this would create a circular reference. What I want to do here is query my join table and get all the User_IDs that have an associated Event_ID equal to the ID of the Event I am serializing.
How can I do this?
I don't know exactly what you want to end up with in your JSON, but I suspect you want to select into a new anonymous type and serialize that instead. Something along these lines maybe:
from e in myContext.Events
where e.ID = 123
select new {
Event = e,
UserIDs = (from u in e.Users select u.ID)
}

Categories

Resources