How to make a database consult using a date and time variable? - c#

I have the following line code used to retrieve data from database:
DataTable newEquipmentTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID FROM Equipment WHERE Name = '{0}' AND CreatedOn = '{1}'", equipment.Name, equipment.CreatedOn.ToString("yyyy-MM-dd HH:mm:ss.fff")));
It returns an error saying "The conversion of varchar data type to a datetime datatype resulted in an out-of-range value"
When I make a direct consult, using the following query it returns the expected result.
SELECT ID FROM Equipment WHERE Name = 'aa' AND CreatedOn = '2012-04-17 19:42:49.650'
What am I doing wrong?

You're passing a String value to select a datetime field. So you need to convert it to a datetime first:
Convert(datetime,'2012-04-17 19:42:49.650', 102)
Apart from that you might be open for SQL-Injection as #Mr47 has mentioned, use SqlParameters.

Please note that SQL's datetime type cannot hold values prior to 1753-01-01.
Just inspect the SQL string with the debugger before it is used to query the database in order to find the problem.
However, the best thing to do is to use a parameterized query (like in this example); otherwise you're vulnerable to SQL Injection.

What is the value of equipment.CreatedOn?
I think problem with the culture setting either in sql-server or your application. It cannot directly covert your date. User culture setting will solve problem.

Related

.Net DateTime cannot be saved to SQL Server DATETIME because of precision

I have a property on an class that is of the .Net type DateTime. It is attempting to save into a table in SQL Server 2008 with a type of DATETIME. I am receiving a Database Error when I attempt to save a new record to the table from my .Net service.
When I look at SQL Server Profiler and see the call to the Stored Procedure that saves to the table, the property is a string: '2014-09-04 23:08:18.0500000'. When I truncate this string to just milliseconds the Stored Procedure call succeeds. The conversion of my .Net DateTime property to this string all happens under the hood and I have no control over that.
I do not need the full precision that I am seeing in the string, but it is important to keep milliseconds. I would rather not change my table column to a data type of DATETIME2. How can I remove the extra precision from the .Net DateTime property?
DateTime dateTime;
dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
Please have #Tanner reply and mark that as an answer if correct as I believe he is correct in his comment above. Be careful to check for NULL on DateTime as you cannot convert to formated string if no data exists.
Below explains the range of DATETIME field in SQL Server.
http://msdn.microsoft.com/en-us/library/ms187819.aspx

Equality comparison fails between C# datetime and SQL datetime

I am trying to do an equality comparison between a C# datetime and a SQL datetime, which appears to fail due to the precision in which those values are stored.
So say I have a really simple SQL table, with these fields:
ID (PK, int, not null) - identity
Title (text, null)
UpdateTime (datetime, not null)
I create a class object via Entity framework, setting its UpdateTime to DateTime.Now like so:
DateTime now = DateTime.Now;
Title.UpdateTime = now;
When I insert my new object into the table, I see that all the values appear to be stored correctly. Now I want the ID that was created for my object.
This is where things get borked. I try pulling the ID back via LINQ:
Title.ID = context.DBTitles.Where(x=>x.UpdateTime == now).FirstOrDefault().ID;
This throws an exception because the query returns null, despite the fact that the 'now' I've inserted is supposedly the same as the 'now' that was inserted into my table.
So my question : How do I ensure that my C# datetime is equivalent to the datetime stored in SQL? What is the best way to get my ID back?
If you are using SQL server, use DateTime2 instead. It has the same precision as the .NET type.
If you're using Linq, won't the object you're inserting get the ID assigned to it upon commit? You won't need to 'recall' it.

How to convert SQL Server timestamp object to string

I need your help in small problem, I have a column (data type timestamp) in SQL Server 2008.
Now I want to show this timestamp value in ASP.Net C# app as string. Is there any way to do that?
I tried it using regular data fetching in ASP.Net but it produced System.byte[] as output rather than actual value. In SQL Server Management Studio values are represented as 0x000000000000B3C0.
One option is to change it to the Date, while getting from the database. Like:
SELECT timestamp = DATEDIFF(s, '19700101', yourTimestampColumn)
FROM yourTable
I don't know if i catch you, but in sql you can cast timestamp value to datetime then to varchar like this:
declare #val timestamp = 0x0000AAE200B29565
select cast(cast(#val as datetime) as varchar(max))

Error when inserting a record into MS Access

I have val MyDate in my C# program that contain today-date or null.
I have date field in my access 2007 - TdateOpen
I try to insert to the database like this:
SQL = "insert into MyCount(TdateOpen) values ('" + MyDate +"')";
and I get this error:
Data type mismatch in criteria expression
what can be the problem?
Coz in your SQL statement you are entering date as String . Instead of String it should be a date/date format.
Try to surround by # .
You will need to ensure that the date is in US order (mm/dd/yyyy) or ANSI/ISO order, whether you use dash or slash is not important, ANSI/ISO is to be preferred.
Then as, Madhu CM said, the delimiter for dates in Access is hash (#), however, your date can be null and null cannot be delimited, so you will either have to add the delimiter to a date string, if a date is returned, or use two sql statements, one for null and one for date.
You could SQL parameters instead of dynamically embedding the date value into the statement.
SQL = "insert into MyCount(TdateOpen) values (?)";
var parameter = yourCommand.CreateParameter();
parameter.Value = yourDateTime;
yourCommand.Parameters.Add(parameter);
(DISCLAIMER: The code was not compiled nor tested, but it should give you a hint)

Getting DB Server DateTime in application

I am using Entity framework and have 1 field in database AddedDate that is DateTime and not null, so I need to pass DateTime value.
But the problem is I have to pass DB Server datetime. How can I manage in this sceario or how can I get DB Server datatime to pass this.
I need to some unique solution, because I am this on many forms.
Edit: I need DB server Datetime upon insertion/updation in my application so that I can pass to entity framework object.
Thanks
Since you are using entity framework, you can do something like this:
var dateQuery = yourDbContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime dateFromSql = dateQuery .AsEnumerable().First();
In general, if you use the entity framework and you use DateTime in a field, it will automatically do the back/forth conversion for you, just the same way it does so for integers, doubles etc.
Unless you mean something special, i.e., a char[40] field that must be filled with a DateTime value of a particular format.
You can get database server date and time by running SELECT GETDATE()) script.
Consider you have a table with 4 colums - the first 3 being strings and the last datetime, You can solve your issue by issueing INSERT SQL like this:
INSERT INTO myTable VALUES ('x', 'y', 'z', SELECT GETDATE())
Can't you use a stored procedure so you can get DB server Datetime very easily.
Just use getdate() in your query. For example:
INSERT INTO your_table (AddedDate, ...other columns) VALUES (getdate(), ...other values)
This basically asks the server to insert its own current date into the field; there's no need for you to retrieve it locally.

Categories

Resources