c# - DateTime format into SQL Server -
I am trying to insert a SQL Server database into a C # application.
I am in C # using It gives me The SQL Server table contains the datatype Although the value is going to be However when I use (con) {con.Open ()) using It Fails It says, A varchar data type is an out-of-range value as a result of a datetime data type conversion. This statement has been aborted. I think this is because Visual Studio 2010 and SQL Server have a different day for each other. Time format How do I fix this? Current code: Always use SQL-Parameters instead of string-concatenation - This prevents such issues from being more important - - From SQL injection: You: " If I was not using datetime.now and got a date time from the value entered by the user. '24 / 07/2014 10 : 30 'How do I use AddWithValue to get it? " You must first parse the input on the For example: datetime.now to get the current date time:
order.PendingDateTime = DateTime.Now;
25/07/2014 11:30:17 .
datetime . For example,
2014-07-23 14: 54: 01.607 keeps data as
25/07/2014 11:30:17 using a common inserted script it fixes in the SQL Server table but in the table
2014-07-25 11:30:17 Display as . (It's OK)
SqlConnection
; (SQL Command cmd = new SQL Using Command ()) (Simdikknekshn = con; Cmd.CommandText = @sql; cmd.ExecuteScalar ();}}
string sql = "include in command ([Ledgerger ], [Order number], [OrderDate], [PendingDateTime], [EmailAddress]) value (1, '' + rec.OrderNumber + '' changed (date, '' + rec.OrderDate + '', 120) changed) Date, '' + REC .PendingDateTime + " ', 120),'" + rec.EmailAddress + " ')"; try {SqlConnection con2 = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings [ "OrderContext"]. ConnectionString) ; Use (con2) {con2.Open (); (Using SqlCommand cmd = new SqlCommand ()) (Simdikknekshn = Con 2; Cmd.CommandText = @sql; Cmd.Parameters.AddWithValue ( "rec.PendingDateTime", DateTime.Now); Cmd.Parameters.AddWithValue ( "Rec.OrderDate", rec.OrderDate); cmd.ExecuteScalar ();}
string sql = @ "in order to insert ( [LedgerNumber], [ORDERNUMBER], [OrderDate], [PendingDateTime], [email address]) value (1, @andnumber Su, @ Ordrdet, @ Pandingdet, e-mail address) "; (var con = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings [" OrderContext "]. By using the ConnectionString)) (SqlCommand cmd = new SqlCommand (SQL, Thieves)) {cmd.Parameters.AddWithValue ("@ ORDERNUMBER", REC order number); Cmd.Parameters.AddWithValue ("@ OrderDate", rec.OrderDate); Cmd.Parameters.AddWithValue ("@ Pending Time", rec.PendingDateTime); Cmd.Parameters.AddWithValue ("@ Email Address", rec.EmailAddress); Con.Open (); Cmd.ExecuteNonQuery (); }
DateTime so the
DateTime Use parse or
DateTime.TryParse ,
DateTime.ParseExact or
DateTime.TryParseExact .
TryParse -methods enables you to check that the input is a valid
DateTime .
date time pending time; If (DateTimeTrappers (TestPendingDateTime, Text, Pending BindTime)) {Message Box. Show (Enter a valid pending date in the format: your format "); Return; } // Here you can go with code above
Comments
Post a Comment