Calculate datediff using mysql in c# winform project - c#

I'm creating a BloodBank application using c# and MySQL and my trouble right now is that I want to create a query with DATEDIFF() function that can calculate the difference in days between a made donation already and a new one from the same person since the same person can only donate blood 60 days after a previous donation. One of the dates is already in my MYSQL database and the other one will be received from a datepicker in a Window form .Net.
I have problem making the connection between the mysql database row for the current person and the info in the new made donation.
At the end I want to check if the difference between donations from a same person(same name and email in my case) is more than 60 days and in that case he can make a donation again.

First thing that comes to mind is that instead of using a DATEDIFF() function in a query, pull the Date information from the database and put it into a DateTime variable, then run a comparison based on DateTime.Now. If it's more than 60 days, it's good to go, otherwise they can't donate.
Additionally, consider using something other than a name/email combo as your check. Between work and personal emails, I probably have like 5 accounts that I would be able to use within your system.

Related

How to insert credit card expiration date in insert query in asp.net

I need to insert expiration date of credit card into database.
But i have only date and year dropdown.There is no dropdown for month.
If i am using this way then it gives error,because without month dateformat is wrong.
cmd.Parameters.AddWithValue("#ExpirationDate", dobday.Value & "-" & dobyear.Value)
Please suggest me how i can insert this in database.
You have three options.
The first is to modify your database so that the expiration date is a char(4). Then store it as MMYY. You don't need the dash or even day part for processing.
The second option is to modify your query so that you pass the day part as "1". So a CC that expires in December or 2012 would be 12/1/2012. Of course, your code should drop the day when you are doing something with it.
Personally, I'd go with option three. Don't store it at all. There is simply zero reason to store cc details in any database. Nearly all CC transaction providers provide much better ways of handling recurring transaction where your system doesn't have to keep that info around. If you are working with one that doesn't, then change providers as they are way behind the times. Otherwise you are playing with fire.
(from comments)
datatype of columns is datetime but here i am trying to insert by making them string
Yeah, don't do that. If the datatype is datetime, then construct a DateTime:
var expiry = new DateTime(/* whatever you need here using dobday / dobyear */);
cmd.Parameters.AddWithValue("#ExpirationDate", expiry);

database design for complex time range

I have a reservation of tickets on a site.
I need to save time, after which I must drop the reservation.
I have a simple condition:
drop reservation after 30 minutes after reservation
and complex:
from 00:01 to 18:00 - drop after 20 hours, from 18:01 to 00:00 - drop after 16 hours.
How to design database for this?
Now I have simple TimeSpan field in a C# class. And, it's enough for simple rule, but not for complex rules.
Thanks.
PS. The DB is MS SQL Server 2008
You don't want to be controlling the time frame with the server and SQL. This should be done in your application. So for each reservation you create a timestamp (stored in the database as column ExpiryTime - or whatever you want). Periodically you check the column ExpiryTime to see if a given reserveation has timed-out or not. if it has perform the operation to remove the reservation...
I hope this helps.

best way to split up a long file. Programming or SQL?

I have a database Table (in MS-Access) of GPS information with a record of Speed, location (lat/long) and bearing of a vehicle for every second. There is a field that shows time like this 2007-09-25 07:59:53. The problem is that this table has has merged information from several files that were collected on this project. So, for example, 2007-09-25 07:59:53 to 2007-09-25 08:15:42 could be one file and after a gap of more than 10 seconds, the next file will start, like 2007-09-25 08:15:53 to 2007-09-25 08:22:12. I need to populate a File number field in this table and the separating criterion for each file will be that the gap in time from the last and next file is more than 10 sec. I did this using C# code by iterating over the table and comparing each record to the next and changing file number whenever the gap is more than 10 sec.
My question is, should this type of problem be solved using programming or is it better solved using a SQL query? I can load the data into a database like SQL Server, so there is no limitation to what tool I can use. I just want to know the best approach.
If it is better to solve this using SQL, will I need to use cursors?
When solving this using programming (for example C#) what is an efficient way to update a Table when 20000+ records need to be updated based on an updated DataSet? I used the DataAdapter.Update() method and it seemed to take a long time to update the table (30 mins or so).
Assuming SQL Server 2008 and CTEs from your comments:
The best time to use SQL is generally when you are comparing or evaluating large sets of data.
Iterative programming languages like C# are better suited to more expansive analysis of individual records or analysis of rows one at a time (*R*ow *B*y *A*gonizing *R*ow).
For examples of recursive CTEs, see here. MS has a good reference.
Also, depending on data structure, you could do this with a normal JOIN:
SELECT <stuff>
FROM MyTable T
INNER JOIN MyTable T2
ON t2.timefield = DATEADD(minute, -10, t.timefield)
WHERE t2.pk = (SELECT MIN(pk) FROM MyTable WHERE pk > t.pk)

asp.net mvc datetime save to different db table depending on date

I want to save entries to a database table depending on the DateTime entered. I have a different model and partial view for each month in the year. Users can create events, I want the events to be saved to the corresponding month table in the db so I can return it to the correct view.
So I need some sort of if statement that says 'if the month value of the entered DateTime is x save to tabel x, if y save to y' and so on.
The user will navigate from month to month and pick dates from a html table styled like a calendar, so the entries need to be inserted to the section of the calendar that corresponds to the datetime. This is just to explain why I need this functionality.
If someone can reccomend a more elegant and functional method of achieving this, go right ahead!
I'm sorry I don't have code to post, I have tried a number of ways and failed. I will post code tomorrow morning when I'm at my computer, but this seems quite simple and I'm very new to this lark so if someone could shed light now, that would be great.
Thanks in advance!
Don't break it out into 12 separate tables. Store them in a single table. You could create a computed column that tells you the month number by calculating it from the DateTime value you're already storing. If your REALLY need to, you can create 12 views off of this table rather easily but I'd take the approach of adding time parameters to your query's WHERE clause. Make sure you index on the DateTime column.

DataTables, DataSets, Entity Framework, LINQ & Lambda Expression for Financial Technical Analysis C#

this is more of an architectural question more than a specific code problem as I've hit a major block in how I am going to proceed with this project.
I'm building a financial scanning software that filters stock picks on specific criteria, for example. For example if out of 8000 stocks, its closing price today is above the SMA 100 and 10 days ago the closing price is below the SMA 100, then return this stock Symbol back to me.
However, note that the SMA (Simple Moving Average) is calculated with the last 100 days of data in the above example, but it could be that we could change the 100 days for lets say another value, 105 or 56 - could be anything.
In my Database I have a table definition called EODData with a few columns, here is the definition below;
EODData
sSymbol nvarchar(6)
mOpen money
mClose money
mHigh money
mLow money
Date datetime
The table will hold 3 years of End Of Day Data for the American Stock Exchange so that is approximately 6,264,000 rows, no problem for MS SQL 2008 R2.
Now, I'm currently using Entity Framework to retrieve data from my database, however what would be the best way to run or create my filter because the SMA must be calculated for each Symbol or underlying Stock Ticker each time a scan is performed because the 100 day variable can change.
Should I convert from Entity Objects to a DataSet for in memory filtering etc???
I've not worked with DataSets or DataTables much so I am looking for pointers.
Note that the SMA is just one of the filters, I have another algorithm that calculates the EMA (Exponential Moving Average, which is a much more complicated formula) and MACD (Moving Average Convergence Divergence).
Any opinions?
What about putting the calculations in the database? You have your EODData table, which is great. Create another table that is your SummaryData, something like:
SummaryData
stockSymbol varchar(6) -- don't need nvarchar, since AMSE doesn't have characters outside of normal English alphabet.
SMA decimal
MCDA decimal
EMA decimal
Then you can write some stored procedures that run on close of day and update this one table based on the data in your EODData table. Or you could write a trigger so that each insert of the EODData table updates the summary data in your database.
One downside to this is that you're putting some business logic in the database. Another downside is that you will be updating statistical data on a stock symbol that you might not need to do. For example, if nobody ever wants to see what XYZZ did, then the calculation on it is pointless.
However, this second issue is mitigated by the fact that 1. you're running SPs on the server which MSSQL can optimize and 2. You can run these after hours when everyone is at home, so if it takes a little bit of time you're not affected. (To be honest, I'd assume if they're calculations like rolling averages, min/max etc, SQL won't be that slow.)
One upside is your queries should be wicked fast, because you could write
select stockSymbol from SummaryData where SMA > 10 you've already done the calculation.
Another upside is that the data will only change once per day (at the close of the business day) but you might be querying several times throughout the day. For example, you want to run several different queries today for all the data up to and including yesterday. If you run 10 queries, and your partner runs the same 10 queries, you've just done the same calculation over. (Essentially, write once, read many.)

Categories