problem with date in sql CE - c#

i have sqlCE DataBase, i have Tdate field (datetime)
i notice that his format is: MM/DD/YYYY
in my C# program i work with date in: DD/MM/YYYY format.
how i can insert to my sqlCE data base my C# format ?

i have Tdate field (datetime) i notice
that his format is: MM/DD/YYYY
No, the datetime field doesn't have any format. The format is determined when the datetime value is converted into a string after reading it from the database.
The same works when you insert a datetime value in the database, you use a datetime value, not a string. If you supply a string value to the database, it will try to parse it into a datetime value. Whatever format you use when you insert the datetime value, that doesn't affect how the value is returned when you read it, as only the value is stored in the database not the format.
When you insert the value you should use a parametererized query so that you supply the date as a DateTime value, not as a formatted string in the query.
When you read the data from the database, you get a DateTime value. You can either set the culture of the application to control how the default conversion from DateTime to string is done, or use a specific culture or format when you convert it.

Maybe there's some function, i'm not sure. My variant is:
string DateString = "25/10/2009";
DateTime dateTime = DateTime.Parse(DateString);
string sqlString = datetime.Month+"/"+dateTime.Day+"/"+dateTime.Year;

I think you don't need to worry about the Date Format.

Related

Date is not taking proper format as Expected (dd/MM/yyyy)

I want to insert date into database in dd/MM/yyyy format. For that I have written like below:
drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]);
And it is working perfectly fine on my local machine, but on my server it is taking format as dd/MM/yyyy hh:mm:ss. So how to set the same format there too. Kindly suggest.
Use DateTime.ToShortDateString:
drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]).ToShortDateString();
But, I suggest you keep the time part of your date (without using the ToShortDateString as it will insert a time set to midnight, cf. highlighted text in Oracle documentation below) when inserting and get the format you want (without time) when you're using the date.
From Oracle documentation:
Oracle Database automatically converts character values that are in the default date format into date values when they are used in date expressions.
If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.
Oracle Database DATE columns always contain fields for both date and time. If your queries use a date format without a time portion, then you must ensure that the time fields in the DATE column are set to midnight. You can use the TRUNC (date) SQL function to ensure that the time fields are set to midnight, or you can make the query a test of greater than or less than (<, <=, >=, or >) instead of equality or inequality (= or !=). Otherwise, Oracle Database may not return the query results you expect.
So you can convert you're date in any format you want in c#, removing the time part, Oracle will automatically set the time part of your date as midnight (00:00:00).
You can set CultureInfo;
var cultureInfo = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime(e.Record["CHEQUE_DT"], cultureInfo).ToString();
Check this Discussion
Here you can find two or more ways to setting date format while insert records into database.
insert into sampleDate(Start_Date) values (to_date('25-Jun-2017','YYYYMMDD'))
Try:
public static DateTime? DateFromString(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
else
{
return DateTime.ParseExact(dateString, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
}
DateTime? date=DateFromString(e.Record["CHEQUE_DT"]);
drExpInfo[0]["CHEQUE_DT"] = date==null? DBNull.Value : (object)date);

OleDb return Date Column value with time

I have Excel File(.xls) which contain date column without time value. I am trying to read this file and put it in datatable with connection string as below:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test\Desktop\1.xls; Extended Properties="Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text"
Excel File Data
OleDb return datem value with time. 7/20/1995 12:00:00 AM
DataTable Data
But i want datem column value without time. Anyone have idea what i have to do to solve my problem.
Thanks.
.Net framework's DateTime consist of Date and Time. You can't really separate Time part from DateTime, instead you can use custom format for displaying records.
string str = DateTime.Today.ToString("MM/dd/yyyy");
I assume it's just a display issue in the DataSet visualizer. Actually it's a DateTime column which has no inherent format. So if you want to output a DateTime only with the date part:
string date = dt.ToShortDateString(); // or dt.ToString("d")
The Short Date ("d") Format Specifier

C# DateTime Convert from String

I am reading excel file in my C# .net web application and storing that value in Database. Ony of field in Excel is DateTime. I am storing it in string
string tran_time = Convert.ToString(odr[5]); //tran_time is "03-11-2012 16:08:43"
and then convert it in in DateTime and store it in Database (SQL Server 2008)
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(tran_time, "dd-MM-yyyy HH:mm:ss", culture);
But the Value Being stored in Database is in format
2012-05-11 13:40:23.000 (yyyy-mm-dd)
and the value in Excel is 05-11-2012 13:40:23 (dd-mm-yyyy)
Date & Month is get replaced.
My Question is How can i store it in Database in Format (YYYY-MM-DD HH:MM:SS:FFF)
But the Value Being stored in Database is in format
No it isn't (assuming that you are using a DATETIME column type). This is just what the SQL tools show you.
A DateTime instance, either in a database or in C# does not have an associated format. It only gets formatted when displayed to the user.
Or use yyyy-MM-dd format. Works like a charm:
string myString = dateVal.ToString("yyyy-MM-dd");
Use dd-mm-yyyy instead of dd-MM-yyyy.

DateTimeFormat issue when pass the parameter to MS-Sql stored procedure

I have a stored procedure, which has a parameter like #CurrentDate datetime, when I pass the value DateTime.Now from the front end (C#) to this stored procedure it is working fine.
But when I change the Date Format as English(India) in my system/server, that time DateTime.Now will return a value like 25-10-2012 PM 05:23:27. I am passing this value to stored procedure and I'm getting an error message like the following,
Msg 242, Level 16, State 3, Line 10
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Note : I don't want to use GETDATE() from Sqlserver, I want to pass the parameter from c#.
How can I solve this?
The problem with the date format is because you are converting it to a string and then back to a DateTime value. Depending on the culture settings on the specific server this may or may not work. It may also misinterpret the data, e.g. transforming a date from 2013-10-12 to 2013-12-10.
DateTime datet = new DateTime(year,month,day);
startDateParam.Value = datet;
endDateParam.Value = datet;
"note that the server stored datetime with format '1900-01-01 00:00:00.000'"
You can use this conversion from C#. Hope this helps
DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)
You will need to convert the value to a valid DateTime using Convert function.
For more information : http://www.sql-server-helper.com/tips/date-formats.aspx
Storing dates in local timezones can cause headache when you launch internationally.
If you save current date using GETUTCDATE() on SQL Server side, you can get the time in user specific timezone as follows:
public DateTime GetDateByTimeZoneId(DateTime dateTime, string timeZone)
{
if (dateTime == null)
{
return null;
}
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(timeZone));
}
You can then pass user relevant timezone like "GMT Standard Time"

Propagate a DateTime column of a DataSet to database

I have a DataSet that has a timestamp (i.e. Datetime.Now ) recording the date and time when a row is added to the DataSet in memory. I will later save (or propagate) all these rows to the SQLCE database where the DataSet's timestamp will be propagate to a DateTime column in the database table.
It works fine and datetime comparison is also ok:
dataView1 = new DataView(
dt,
"DataTime >= '6/9/2011 5:00:20 PM'",
"Data_ID ASC",
DataViewRowState.CurrentRows);
The above code works ok, but I worry if the program runs on different computer (i.e. another language of Windows) the Datetime.Now format would have a different format, or if I compare data that is record from a different computer, the different format of the DateTime in the database will causes it to fail. Would this problem happen? Or is there a safer way of doing this?
String datetime = String.Format("{0:G}", ur_datetime_variable);
will give you string in M/d/yyyy h:mm:ss tt format and then convert the datetime string to DateTime
here's a link http://www.csharp-examples.net/string-format-datetime/ and there's everything bout datetime formating.
Should you not use CultureInvariant
DateTimeFormatInfo.InvariantInfo
when parsing dates

Categories

Resources