I've lately been migrating from Delphi to C#. I find C sharp very powerful and the IDE is awesome. There are some unaccustomed stuff though.
On the database side I have two inner-related tables. Students and Categories. I keep CategoryID in the students table as foreign key. I want to link a DataGridView to the Students table.
When I add a new student I want to be ale to also choose the student's Category from the dropdown list that list of available Categories. In Delphi we have an opportunity to add a lookup column to table so that it keeps CategoryID in the backgound but displays CategoryName.
How do I do it in C#? Is it possible through DataRelation? If yes, how?
... You don't mention data technology (Linq etc.) or interface (e.g. Winforms, Webforms etc,) so it is far too hard to help you....
But I have done something similar and I would treat it as two queries, first do a select category.name to populate the drop down box, then something along the lines of
int _temp = select category.id where category.name == dropdownbox.selectedtext
then in your insert command for the student, you simply provide the int _temp.
Related
I have two tables, table1 has a foreign key of table2. I want to get some info from table1 including a column which both table1 and table2 have. Should I join these two table to get the info?
For example:
I have two tables Course and Student, such as below
enter image description here
Solution1:
Course
|project CourseId, CouseName, StudentName, StudentId
Solution2:
Course
| project CourseId, CourseName, StudentName
| join Student
on $left.StudentId == $right.StudentId
| project CourseId, CourseName, StudentName
To get the CourseId, CourseName and StudentName, which solution is correct? Is it a good practice to pick solution2?
Please ignore the table design. It is just an example. The issue is coming from a real project that we need a query from one table which contains a foreign id and common column from other table. The query should give the common column back. Is it necessary that join the second table to get the common column?
Course table should not have Student name, it’s redundant data. And yes you can use inner join or left join on student id. Just bear in mind with the given information provided by you, the case where student id can be or cannot be null in course table.
I believe the concept of join should only be used when we need data from 2 or more tables. If you have all the required data in a single table, why you need a join. If were you, I will surely move forward with first solution. Thanks
Your tables do not follow those three normalization forms.
This is how entities and relationships should be organized if we are talking about the common scenario of designing a database for a school or something something similar to this.
There is a Many to Many relationship between Courses and Students, which allows us to associate N students with one course, and M courses with one student (one course can be attended by many students, and one student can attend many courses).
Also, this design follows the 3rd Normalization Form.
Learn more about normalization forms
Learn more about Many to Many relationships
I am building a windows form C# app. and I use oleDb for linking access database to my app. the problem is, My access database has two tables (students,courseCodes) and one column of the "students" table(courseName) is linked to one in the "courseCode" table (the "courseCode" table contains course codes for example course code 1 is Static and I use code 1 in the "students" table for displaying Statics) now when I want to select column containing Statics using
"SELECT DISTINCT courseName FROM students";
I got the "1" instead "Statics" is there any way to retrieve "Statics" instead "1"?
I'd say your naming convention is misleading and confusing. The column should be courseIndex, not courseName.
Do a JOIN, of course (no pun intended). This query will return the distinct course names that a given student has signed up for.
select distinct courseCode.courseName
from student
join courseCode
on student.courseId = courseCode.id
where student.id = ?
Please adjust for your schema details.
Personally I think this is a poor design. A student can sign up for many courses, and a course can have many students. This is a many-to-many relationship. You need a join table; sounds like you only have a foreign key one-to-many relationship here.
I am creating a billing software. I have created a database having 4 tables.
Category_Master
Product_Master
Customer_Master
Order_Details.
I am confused at creating rows in Order_Details. The reason is that, if a customer purchases 10 different items, then each items ProductCode should also be added to the Order_Details table.
So the thing is that do i need to create rows for each and every products or is their any other way to represent all ProductCode in a single cell.
I would recommend you to slit your Order_Details into two tables:
OrderProduct
OrderID | ProductCode
Order_Details
OrderID | OtherParameter
Each product of the order should be a new row in OrderProduct table. This structure will allow you to store order details separate from Products, connected with this order. The OrderProduct table would contain only links of your products with the orders in relation of many to many. Joining of these tables would allow you to make any required Select queries.
You need to create rows. Although there are alternatives that are technically possible, all of them are incredibly bad design and an uneducated hack at best.
I would also suggest you to create one more table (Order_Products) to manage the products ordered under one order, So that you will be able to easily track the products ordered under one order. Managing multiple values using a single cell can be used if the multiple values are of a constant size, such as days of a week which can be managed by using a binary field, But in your case the number of products is a variable and so i prefer using another table for doing the same.
Thank you.
I have two tables..
Students
StudentID
Name
SchoolID
School
SchoolID
SchoolName.
This is my scenario, I have created a DetailsView on my page which displays X StudentID and its columns. When displaying the SchoolID (from Students table) column it will display the actual ID as its supposed to.
I would like to know how I can display the actual school name instead of the schoolID, what I did first was simply have a query inside the first query which displays the name of the school like...
SELECT StudentID, (SELECT SchoolName FROM Schools
WHERE Schools.SchoolID = Students.SchoolID)
FROM Students WHERE StudentID=1
Although this works this creates issues when trying to edit the record via GridView, it will not save the correct data in those fields.
In the edit step in GridView I'm able to bind the SchoolID key with SchoolName, however this will NOT work when having a query within a query.
So my question really is how can I display foreign key data within Visual Studio without changing the SQL command?
The solution previously was to edit the Select command (after the SQLDatasource was created) to perform a inner join on the table containing the meaningful field name.
Check Here
1.Link
2.Link
You could do that with join query, example:
select students.StudentId,students.Name,students.SchoolID,school.SchoolName from students join school on students.SchoolID=school.SchoolID
And, for better understanding of "JOINS" here is useful link: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
Out of my lack of SQL Server experience and taking into account that this task is a usual one for Line of Business applications, I'd like to ask, maybe there is a standard, common way of doing the following database operation:
Assume we have two tables, connected with each other by one-to-many relationship, for example SalesOderHeader and SalesOrderLines
http://s43.radikal.ru/i100/1002/1d/c664780e92d5.jpg
Field SalesHeaderNo is a PK in SalesOderHeader table and a FK in SalesOrderLines table.
In a front-end app a User selects some number of records in the SalesOderHeader table, using for example Date range, or IsSelected field by clicking checkbox fields in a GridView. Then User performs some operations (let it be just "move to another table") on selected range of Sales Orders.
My question is:
How, in this case, I can reach child records in the SalesOrderLines table for performing the same operations (in our case "move to another table") over these child records in as easy, correct, fast and elegant way as possible?
If you're okay with a T-SQL based solution (as opposed to C# / LINQ) - you could do something like this:
-- define a table to hold the primary keys of the selected master rows
DECLARE #MasterIDs TABLE (HeaderNo INT)
-- fill that table somehow, e.g. by passing in values from a C# apps or something
INSERT INTO dbo.NewTable(LineCodeNo, Item, Quantity, Price)
SELECT SalesLineCodeNo, Item, Quantity, Price
FROM dbo.SalesOrderLine sol
INNER JOIN #MasterIDs m ON m.HeaderNo = sol.SalesHeaderNo
With this, you can insert a whole set of rows from your child table into a new table based on a selection criteria.
Your question is still a bit vague to me in that I'm not exactly sure what would be entailed by "move to another table." Does that mean there is another table with the exact schema of both your sample tables?
However, here's stab at a solution. When a user commits on a SalesOrderHeader record, some operation will be performed that looks like:
Update SalesOrderHeader
Set....
Where SalesOrderHeaderNo = #SalesOrderHeaderNo
Or
Insert SomeOtherTable
Select ...
From SalesOrderHeader
Where SalesOrderHeaderNo = #SalesOrderHeaderNo
In that same operation, is there a reason you can't also do something to the line items such as:
Insert SomeOtherTableItems
Select ...
From SalesOrderLineItems
Where SalesOrderHeaderNo = #SalesOrderHeaderNo
I don't know about "Best Practices", but this is what I use:
var header = db.SalesOrderHeaders.SingleOrDefault(h => h.SaleHeaderNo == 14);
IEnumerable<SalesOrderLine> list = header.SalesOrderLines.AsEnumerable();
// now your list contains the "many" records for the header
foreach (SalesOrderLine line in list)
{
// some code
}
I tried to model it after your table design, but the names may be a little different.
Now whether this is the "best practices" way, I am not sure.
EDITED: Noticed that you want to update them all, possibly move to another table. Since LINQ-To-SQL can't do bulk inserts/updates, you would probably want to use T-SQL for that.