Different format for timestamp when using c# datareader - c#

I'm reading a timestamp from a mysql table using an OdbcDataReader. When I look at the data in the table it is in the format 2013-09-12 11:11:09. But the reader seems to read it in the format 12/09/2013 11:11:09.
I then try to insert this into another mysql table but receive the error:
Incorrect datetime value: '12/09/2013 11:11:09' for column 'timestamp'
at row 1
How can I sort out this difference in formatting? Should I be referencing some Unix timestamp value somehow?

The data shouldn't be in the table in any text format. It's just a date and time.
You'll see the format when you convert the data to a string - which you should do as rarely as possible. In particular, when you're inserting the data into a different table, you shouldn't use a formatted value at all - you should use a DateTime in parameterized SQL.
Basically, unless you really need a string representation of the data, you should keep it in the "native" representation (DateTime in this case). Every time you have a conversion to or from text, that's an opportunity for failure. Dates and times are hard enough with time zones etc, without extraneous conversions getting involved.

How are you looking at the data "in the table"? I'm not familiar with the MySQL implementation, but with Oracle and Sql Server datetime values are stored in an unreadable binary format, and translated to a readable timestamp by the query tool. MySQL is likely doing something similar.
try to insert this into another mysql table
If you care about format when you're inserting the data, you're doing something really bad. That's a strong indication you're using a technique that will be vulnerable to sql injection attacks, rather than parameterized queries. If you use parameterized queries, you assign a C# datetime type to the query parameter value directly, and the ADO.Net object handles any formatting you need. At that point, anything you can successfully DateTime.Parse() or DateTime.TryParse() becomes a valid input for your query.

Related

Datetime format is 'mm-dd-yyyy' when getting while using SqlDataAdapter but if I run the same query in ms sql then its 'yyyy-mm-dd'

I know the question is a bit confusing. Please let me elaborate.
Suppose
I have a table student master which has a column DOB
I have inserted a record and in DOB I have inserted '1991-01-01'
running select statement from sql server is returning date in the same format as it is inserted '1991-01-01' but when I am running the same query from C# using SqlDataAdapter then its returning date as '01-01-1991'
Can anyone explain why it is happening and is there any way to fetch the date in same format as it is inserted.
Query
Is it possible to get the DateTime using SqlDataAdapter as it was inserted?
P.S: column data type is Datetime
let's separate the wheat from the chaff :)
if for your needs meaningful is data type (datetime in this case), then formatting does not matter at all. All layers which will exchange or process the data will use data type information for that.
But
if the meaningful part is formatting, i.e. string representation of the data, then you need to consider the appropriate settings of UI tools you use to display your data. SSMS, for example, uses regional settings for that. If you need to visualize data in the identical manner, so you need the identical strings, you should take care of formatting by your self or in another words, you need to convert your datetime data to string in the same way in all places where you need it.
In T-SQL, for example, you could use CAST and CONVERT functions for formatting your data in a format you need.
If you can't match up the "Cultures" between the SQL Server and the machine you're building the application on (and, in fact, you cannot rely on that really if you're application is going to be deployed to other machines!), then the cheap and quick way round it is to run your date returns through a parse function such as this:
private string FncFormatDate(string date)
{
DateTime formattedDate;
if (DateTime.TryParse(date, out formattedDate))
{
return formattedDate.ToString("yyyy-MM-dd");
}
else
{
return "Invalid date";
}
}
I hope this answers your question.

DateTime conversoin for DB querying

I need to convert DateTime to string for purposes of DB querying.
I thought to do something like
_endTime.ToString(_isoDateTimeFormat.UniversalSortableDateTimePattern)
It works with MySQL, but SQL Server causes problems.
The final string looks like 2012-03-01 15:59:00Z seems z not supposed to be there.
Any suggestions?
You shouldn't be performing a text conversion at all.
You should be storing the data as a DATETIME (or whatever the corresponding type is in the database) and then you should be specifying the value in the query using a parameter, not including it in the SQL.
That way you don't need any string conversions in the first place.
Always pass values via parameters unless you have some really, really good reason why you absolutely have to include it in the SQL directly. Using parameters:
Protects you from SQL injection attacks
Removes conversion annoyances like this one
Keeps your code and data more logically separated

Inserting timestamps into SQLite

I've got a table with a column "date-taken TIMESTAMP", but I'm not sure on what format SQLite is expecting. How would I need to format "5/3/1999 10:30 PM" before I can insert it into the column above?
Also, how do parameterized queries help with formatting things like this?
SQLite doesn't have an actual date/time type, it simply uses strings. Valid date/time string formats can be found here, in the "Time Strings" section:
http://www.sqlite.org/lang_datefunc.html
As for the second question, I'm not sure there's anything you can do with paramaters to help here. You'd probably want to just write a function that converts from your expected format to an SQLite format.

How to change date format in mysql stored procedure insert statement "22-12-2010" to "2010-12-22"

I send the registration date parameter to mysql database like "22-12-2010". But my sql date date type is in another format how can I change the date format like "2010-12-22" also I have to insert this into table.
Give code in C#,asp.net code behind either sql query statement!
Use this comprehensive MSDN pages as your guide: Standard Date and Time Format Strings and Custom Date and Time Format Strings.
There are many examples on those pages on how to reformat a date string in C#, and they also provide a good clear explanation on how date formatting works in the DateTime class.
Once you've reformatted your date string in C#, you should be able to pass it on down without needing to use SQL to reformat it.

Preserving SQL DateTime format when reading from database

I have a very simple tool that runs a stored procedure from a database, puts the results into a DataTable, then writes the DataTable to a file via Response. The purpose is to take the data from a table on SQL Server, then use a 3rd-party tool to upload it to an Oracle database.
The problem I encounter is that date is stored, in SQL Server, as such: 2011-05-01 00:00:00.000.
However, when I access it via my SqlDataReader and put it into my DataTable, it ends up formatting as: 5/1/2011 12:00:00 AM.
So I figured I could just explicitly parse it as an OracleDateTime. I have the following column in my DataTable:
records.Columns.Add("Date", typeof(OracleDateTime));
As well as this bit where I am reading the results:
row["Date"] = OracleDateTime.Parse(rdr["Date"].ToString());
I also tried SqlDateTime for kicks, but ultimately, I end up with the same incorrectly formatted string. I just want it to stay the same way SQL returns it in the query - how can this be done?
The date is not stored that way in SQL server; what you see in your ad-hoc queries is a conversion from the internal numeric storage to that format.
When you read it in, you should be converting rdr["Date"] to a DateTime.
If you want that specific format in your code, you should format it using .NET's formatters when you output it.

Categories

Resources