简体   繁体   中英

Moving Access Application to C# front end and SQl Backend

recently my manager gave me a new task, it was to move one of our access application that handles the net weight calculation to C# and told me to use sql as a back end.

so first i moved all the database from access to sql by going to the move data section under database tools and few clicks and all the data was automatically migrated to sql. second i tried to mimic the access forms onto win forms in c sharp third i saw that few buttons were basically reading data directly from the table so i created a modal form with data grid and called the modal form from my main form and the data from the database got loaded.

NOW THE QUESTION ON THE REST OF THE BUTTONS

I have many many queries in the access application and each query is getting referenced by another query, it seems as though the queries are like tables in access, i am not sure how to go about accomplishing this in SQL Server, if i get the query to work i can create a report with out any problem.

I tried creating a view and that was not helping because it can't find the referenced table, stored procedure seems like the way to go but i dont have much knowledge on stored procedures.

Queries From Acesss

The report being generated is called Combined Master Data Report

SELECT [Net Weight Master Data Query].[Unit UPC Base Item], [Net Weight Master Data Query].[Item Description], [Net Weight Master Data Query].[Production Line], [Production Lines].[Production Line Description], [Net Weight Master Data Query].[Preset Number], [Net Weight Master Data Query].[Weight Factor], [Net Weight Master Data Query].Piece, [Net Weight Master Data Query].[Pcs Per Unit], [Net Weight Master Data Query].[Upper Limit Unit], [Net Weight Master Data Query].[Upper Limit Factor], [Net Weight Master Data Query].[Piece Wt (g)], [Net Weight Master Data Query].[Upper Limit (g)], [Net Weight Master Data Query].[Lower Limit (g)], [Net Weight Master Data Query].[Label Wt (g)], [Net Weight Master Data Query].[Tare Wt (g)], [Net Weight Master Data Query].[Constant Tare Wt (g)], [Net Weight Master Data Query].[Tare Variation Factor (g)], [Net Weight Master Data Query].[Target Wt (g)], [Net Weight Master Data Query].[Reject Wt (g)], [Net Weight Master Data Query].[Repair Min Wt (g)], [Net Weight Master Data Query].[MAV (g)], [Net Weight Master Data Query].[MAW (g)], [Net Weight Master Data Query].[Pkg Length (mm)], [Net Weight Master Data Query].[Film Product Code], [Net Weight Master Data Query].[Film Width (mm)], [Net Weight Master Data Query].[Forming Tube (mm)], [Net Weight Master Data Query].[Type of Jaws], [Overpack Percentages].[Std RM $/LB], [Overpack Percentages].[Avg Overpack Percentage], [Net Weight Master Data Query].[Last Updated]
FROM ([Net Weight Master Data Query] LEFT JOIN [Production Lines] ON [Net Weight Master Data Query].[Production Line] = [Production Lines].[Production Line]) LEFT JOIN [Overpack Percentages] ON [Net Weight Master Data Query].[Unit UPC Base Item] = [Overpack Percentages].[Unit UPC Base Item]
ORDER BY [Net Weight Master Data Query].[Unit UPC Base Item], [Net Weight Master Data Query].[Production Line];

As you can see above the first columns getting referenced is [Net Weight Master Data Query]

Here is Net Weight Master Data Query

SELECT [Net Weight Master Data].[Unit UPC Base Item], [Item Descriptions].[Item Description], [Net Weight Master Data].[Production Line], [Production Lines].[Production Line Description], [Net Weight Master Data].[Preset Number], [Net Weight Master Data].[Package Type], [Net Weight Master Data].[Weight Factor], [Net Weight Master Data].Piece, [Net Weight Master Data].[Pcs Per Unit], [Net Weight Master Data].[Upper Limit Unit], [Net Weight Master Data].[Upper Limit Factor], IIf([upper limit unit]="g","",(CInt(([label wt (g)]/[Pcs per unit])*10))/10) AS [Piece Wt (g)], ([mav (g)]-[scale deviation factor]-[tare variation factor (g)])/[weight factor] AS UL1, [UL1]-Fix([UL1]) AS UL2, IIf([Package Type] Is Not Null,[UL1],IIf([UL2]=0,Fix([UL1]),IIf([UL2]>0 And [UL2]<0.51,Fix([UL1])+[Rounding Factor1],Fix([UL1])+[rounding factor2]))) AS UL3, IIf([Package Type]="Bar",10,IIf([upper limit unit]="g",([UL3]*[weight factor]),[piece wt (g)]*[Upper Limit Factor])) AS [Upper Limit (g)], IIf([upper limit unit]="g",([UL3]*[weight factor]),0) AS [Lower Limit (g)], [Net Weight Master Data].[Label Wt (g)], [Net Weight Master Data].[Tare Wt (g)], [Net Weight Master Data].[Constant Tare Wt (g)], [Net Weight Master Data].[Tare Variation Factor (g)], [Label Wt (g)]+[tare wt (g)] AS [Target Wt (g)], [label wt (g)]-[lower limit (g)] AS [Reject Wt (g)], IIf([package type]="Bar",([Label Wt (g)]-[mav (g)]+[tare wt (g)]),IIf([package type]="10 Pack",([Label Wt (g)]-[mav (g)]+[tare wt (g)])+2.5,([Label WT (g)]-[mav (g)]+[tare wt (g)])+5)) AS [Repair Min Wt (g)], [Maximum Allowable Variations].[MAV (g)], [Production Lines].[Scale Deviation Factor], [label wt (g)]-[mav (g)] AS [MAW (g)], [Net Weight Master Data].[Pkg Length (mm)], [Net Weight Master Data].[Film Product Code], [Net Weight Master Data].[Film Width (mm)], [Net Weight Master Data].[Forming Tube (mm)], [Net Weight Master Data].[Type of Jaws], [Net Weight Master Data].[Last Updated], [Production Lines].[Rounding Factor1], [Production Lines].[Rounding Factor2]
FROM (([Net Weight Master Data] LEFT JOIN [Production Lines] ON [Net Weight Master Data].[Production Line] = [Production Lines].[Production Line]) INNER JOIN [Maximum Allowable Variations] ON [Net Weight Master Data].[Label Wt (g)] = [Maximum Allowable Variations].[Labeled Quantity (g)]) LEFT JOIN [Item Descriptions] ON [Net Weight Master Data].[Unit UPC Base Item] = [Item Descriptions].[Unit UPC Base Item]
ORDER BY [Net Weight Master Data].[Unit UPC Base Item], [Net Weight Master Data].[Production Line];

In the above query [Item Descriptions] gets referenced which is another query

[Item Descriptions] query below

SELECT ItemDescLookups.[Unit UPC Base Item], First(ItemDescLookups.[Item Description]) AS [Item Description]
FROM ItemDescLookups
GROUP BY ItemDescLookups.[Unit UPC Base Item];

So we need to create a giant stored procedure it looks like that references there queries within queries so called sub-queries but there are lot of IFF and other statements like FIRST how do we convert these to SQL. IF anyone can help me create a Stored Procedure i would really appreciated it because all the other buttons create different reports, if i know how to do one i can easily do the same for the others and migrate my application over to csharp.

A "Query" in Access corresponds to a "View" in SQL Server. Just like in Access, a SQL Server View can often be used like a Table in other Views, so in SQL Server you could have

CREATE VIEW ViewX AS
SELECT ... FROM Table1 WHERE ...

and then you could use that View in another View, eg,

CREATE VIEW ViewY AS
SELECT ... FROM ViewX WHERE ...

So you may not need to use a Stored Procedure at all, if all you are doing is recreating the behaviour of saved Queries in Access.

As for the differences between Access SQL and T-SQL (the dialect used by SQL Server), there are several, and they have been discussed many, many times here on Stack Overflow. What you need to do is try converting the SQL statements from Access SQL to T-SQL and if you get stuck on a particular issue then

  1. review How to Ask , and then
  2. ask a new specific question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM