views:

19

answers:

1

I've create object with data type: DateTime But I want save as char data type in sql server

ex: assign value to obj property: MyDateTime=DateTime.Now; //when use to string method result: MM/dd/yyyy hh:mm:ss //save to db MyDateTime column with data type char(12) we will save: MM/dd/yyyy

How can I save it. I try use assign converter attribute to property but does not effect

Update: 2010/09/24 13:36 Ex:
DataContext data=new DataContext();
MyObject obj=new MyObject();
obj.MyDate=DateTime.Now;
data.MyObjects.InsertOnSubmit(obj);
data.SubmitChange();

property's data type of object is: DateTime
column in db is char(12)

how can I format it or write custom attribute to format data before save to db

A: 

Use a string formatter:

string dateString = MyDateTime.ToString("MM/dd/yyyy");

And write that value to the Database.

For other examples, have a look here:

http://www.csharp-examples.net/string-format-datetime/

Russ C