I have 4 columns that act as foreign keys and all are connected to one table which is a detail table for it. I have tried everything and can't get it to work. Instead of saying 1 it should say stefa. Those are names which all have their id's in the other table. I am doing it all in Windows forms the code and query builder, after I run it
SELECT
RadniDan.ID, RadniDan.Datum, RadniDan.PrvaSmena, RadniDan.DrugaSmena,
RadniDan.IspomocPrva, RadniDan.IspomocDruga
FROM
RadniDan
INNER JOIN
Radnik ON RadniDan.PrvaSmena = Radnik.ID
AND RadniDan.DrugaSmena = Radnik.ID
AND RadniDan.IspomocPrva = Radnik.ID
AND RadniDan.IspomocDruga = Radnik.ID)
WHERE
(RadniDan.Datum = ?)
Related
Lets say I have two tables in database, Table1 and Table2. Relationship is one to many from table2 to table1. I would like to show all data from Table1 and not all columns from Table2 on GUI. Foreign key from table2 is column in Table1:
Table2 table2;
I am trying to display data from Table1 and some columns from Table2+foreign key. My problem is how to display foreign key, here is part of code:
List<Table1> list = new List<Table1>();
Table t2 = new Table2();
t2.prop1 = done using OledBReader;
t2.prop2 = ...;
t2.prop3 = ...;
Table1 t1 = new Table1();
t1.prop1 = ...;
t1.prop2 = ...;
*t1.Table2 = t2;*
list.Add(t1);
Two properties of Table2 are name and surname so I am overriding toString(), so that one column will be Name Surname. Foreign key is ID, but I dont know how to display It as a column (I dont want to include it in override method). My problem is that instead of that ID which is foreign key, I have property which is entire object of class Table2, and in Table2 I have to override name and surname, so I dont know where to include this ID, so I will have ID column at the end?
I hope this explanation is helpful.
I have a question about foreign keys in a database. I am programming in c#, using the Entity framework (visual studio winforms) and I have data in my sql database with foreign keys.
I have queries which access these data to get them in a Datagrid. Everything is OK, except I have data in tables which are foreign keys (numbers). When I select them with queries I only get the foreign key (a number) and not the value which is linked in another table.
var requete_reservations = from reservation_spa in bdd.reservation_spa
where reservation_spa.NOMBRE_RESERVATION > 0
select new
{
reservation_spa.CLIENT,
reservation_spa.SPA,
reservation_spa.NOMBRE_RESERVATION
};
dataGrid_reservations.DataSource = requete_reservations.ToList();
In reservation_spa.client I have a number which links another table client
How can I get the Name from client using the foreign keys in reservation_spa?
You must Join table reservation_spa and Client like this :
var requete_reservations = from r in bdd.reservation_spa
join c in bdd.client on r.CLIENT equals c.IDCLIENT
where r.NOMBRE_RESERVATION > 0
select new
{
c.NOM,
r.SPA,
r.NOMBRE_RESERVATION
};
Where is the name for? If you just need the name you could use linq
var name= from c in bdd.Clients //Is that the name of the table of clients?
where c.IDClient= requete_reservations.Client
select c;
I have 3 Tables Customer,CustomerTicket,Ticket
Customer-->ID primary key
CustomerTicket-->ID,TicketNo where ID,TicketNo are foreign-key
Ticket-->TicketNo,Subject where TicketNo primary key
I am using linq to entity and want to show columns like this,
ID TicketNo Subject
1 12 Car
1 18 Home
2 23 Plane
Every unique ID can have Many TicketNo and every TicketNo has one unique Subject
Gridview.DataSource=from customer in entity.Customer
join custicket in entity.CustomerTicket on customer.ID equals custicket.ID .....
I tried code like the above but in the end couldn't understand how to make the table as I want.How will code continue or is there any better way? Also note that entity framework took my CustomerTicket table and add it as navigation property ...
Do the joins, create an anonymous collection with select, create a binding datasource with the collection and set the datasource
var cusList=from customer in entity.Customer
join custicket in entity.CustomerTicket on customer.ID equals custicket.ID
select new
{
custicket.ID,
custicket.ticketno,
ticket.subject
};
var bs = new BindingSource();
bs.DataSource=cusList;
Gridview.DataSource=bs;
I have datatables A and B. Table A has columns 1 and 2. Columns 1 and 2 are the primary key. Table B has columns 1, 2, 4. Columns 1 and 4 are the primary key. I want to update table B so that for every value where B.1 == A.1 I want to make it so that B.2 = A.2. Because 2 is not part of the primary key for table B there may be multiple records where B.1 and B.2 are the same and I want to update 2 for all those rows.
I am stuck at this kind of code:
foreach(DataRow dr in A.Rows){
DataRow Found = B.Rows.Find(dr[1]);
if(Found != null)
Found[2] = dr[2];
}
The major problem I am facing is that because table B has a compound primary key that is shared by table A. The find is looking for two values but only one can come from table A.
I need to update a column in a table which contains a lot of rows. Each row has a some large TEXT columns in it, which i do not need for my update.
I'm using LinqPAD, and this is roughly, what i wanna do:
(from s in Table
where s.FK_ID == null
select new{s.FK_ID, s.Datum, s.PBNummer}).ToList()
.ForEach(s => s.FK_ID = new Guid(...some new guid here...));
SubmitChanges();
This does not compile, as the properties of an anonymous class type are read-only.
If I do
(from s in Table
where s.FK_ID == null
select s).ToList()
then I can update and save, but all columns are loaded, which takes a very long time and causes memory problems.
Is there a way to only load some columns but still have an object that i can update and save using SubmitChanges? Or do i have to switch to SQL statements?
Way to update specific columns of a database record in Linq to SQL is to create a View on the table containing large columns, and only include the “short” columns:
CREATE VIEW [dbo].[V_FooMax] AS
SELECT OID, ID
FROM dbo.FooMax
Since views based on single tables are updatable, an update on the view is performed as an update on the table:
using (var database = new DataContext())
{
var fooView = database.V_FooMaxes
.Where(foo => foo.OID == OID).FirstOrDefault();
fooView.ID = newID;
database.SubmitChanges();
}
Reference: http://devio.wordpress.com/2011/01/15/updating-a-single-column-in-linq-to-sql-using-a-view/
Also you can look at: http://devio.wordpress.com/2011/01/16/updating-a-single-column-in-linq-to-sql-summary/
Firstly, if you don't have a primary key in the database, then you wouldn't be able to update via Linq-To-Sql. If you have a primary key, but just don't know which it is, you can find it in Linqpad by doing something like
var table = (from t in Mapping.GetTables()
where t.TableName == "[Table]" select t).SingleOrDefault();
(from dm in table.RowType.DataMembers
where dm.DbType != null && dm.IsPrimaryKey
select dm.Name)
.Dump("Primary Key");
Once you know the primary key, you can do something like the following, (I'm assuming the primary key is called Id)
var oldList = (from s in Table
where s.FK_ID == null
select new{s.Id , s.FK_ID, s.Datum, s.PBNummer}).ToList() ;
This is similar to your query, except I have added the primary key
foreach(var r in oldList)
{
Table t = new Table();
t.Id = r.Id ;
Table.Attach(t);
t.FK_ID = new Guid(...some new guid here...));
}
SubmitChanges();