1

I am using VSTS 2008 + C# + .Net 3.5 + ADO.Net + SQL Server 2008. I have some C# string type variables, and I want to insert them into database as type DateTime, any code samples?

My problem is how to do the conversion from string to database DateTime type.

2 Answers 2

4

Have a look at the DateTime.Parse() method.

Edit: I suppose you use the SqlCommand, then you just add the resulting c# DateTime object to the Parameters collection.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend the DateTime.TryParse(string input, out variableName)

Something like this:

DateTime safeDateTime;

if(!DateTime.TryParse("2010-09-15 10:00:00", out safeDateTime))
    safeDateTime = DateTime.MinValue;

Like that you will always have a value in the variable that the database can use. Otherwise you could implement a warning if you couldn't parse the date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.