C# Access Database Question - c#

Say i have 2 tables now, PricePlan and Bill. Both tables have a column called 'Price' and I would like the table 'Bill' to update the value from 'PricePlan's Price. How can i do this or what SQL statement should i be using? Thanks in advance!

You will need to have some sort of way to define a relationship between the two tables.
For instance if your tables have this structure:
PricePlan
---------
ID
Price
Bill
---------
PricePlanID
Price
This will only work for SQL Server. See below for Access solution.
Then a query like this should update Bill :
UPDATE b SET b.Price = pp.Price
FROM Bill as b
INNER JOIN PricePlan as pp
ON b.PricePlanID = pp.id
Also, the schema above is only for example purposes. If yours is like that you should look at changing it.
UPDATE
I just noticed this is for Access, sorry. The strucure of your query will be slightly different. See below:
UPDATE Bill INNER JOIN
PricePlan ON Bill.PricePlanID = PricePlan.ID
SET Bill.Price= [PricePlan].[Price];

making some wide assumptions here, but i think this short tutorial on cascading updates in access 2010 should get you on your way.

Related

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 fetch record on the basis of any one value from the column of two comma separated values

May be the Question's title is not correctly defined what i actually wanted to ask. Here is the more specific description of my question
I have a following table User in my database which has a column i.e. Category which contains multiple values but separated by commas
S.no. Name Category
1 Ankit Ex Soldier, Senior Citizen
2 Ritu Widow, Senior Citizen
3 Akash Ex soldier
I wanted to search the record on the basis of category
for eg. If i search
select * from User where Category='Senior Citizen'
Then it must show Ankit and Ritu record.
How to do this.
plz help
try this:
select * from User where Category like '%Senior Citizen%'
You need LIKE operator:-
select * from User where Category LIKE '%Senior Citizen%'
select * from User where Category LIKE '%Senior Citizen%'
But you should use a separate table for Category.
Like Kiss László wrote you should separate the information in two tables. The professional term for this is called "Normalization". Most important to know are the 1NF, 2NF and 3NF (read this for detailed information).
So it should look like the following:
Table Persons
PersonId Name
1 Ankit
2 Ritu
3 Akash
Table Categories
CategoryId Name
1 Ex. Soldier
2 Senior Citizen
3 Widow
Table PersonCategories
PersonId CategoryId
1 1
1 2
2 2
2 3
3 3
Why should you do this?
In my opinion the biggest reason is performance. I made some test table with your current approach with a data set of 20k entries. The execution of the query took about ~200ms to return. With the schema above the following query executed in about ~1ms
SELECT
*
FROM
Persons AS p
JOIN
PersonCategories AS pc ON p.PersonId = pc.PersonId
JOIN
Categories AS c on pc.CategoryId = c.CategoryId
WHERE
c.Name = 'Senior Citizen'
Why is this query so much faster?
Because we can easily use indices on our columns. In the schema above the Persons.PersonId and Categories.CategoryId are the PRIMARY KEY columns of their tables. So to use them as a column for a JOIN operation has minimal costs. Both columns of the PersonCategories table are FOREIGN KEYS (ensures a valid database state and improves performance). Finally the Categories.Name column has an INDEX too.
Could this approach be bad?
In most cases this is the way to go. One reason not to do it this way, is if you have to handle lots of INSERTS. INSERTS in this schema have a much higher cost because all indices need to be updated after the INSERTS.

selecting a column from ms-access database which has linked tables

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.

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?

Reaching child records in a SQL Server table

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.

Categories

Resources