nextmedstaff
asked on
Converting string to Date for sql C#
I have a C# application that loops through an excel file and grabs the values from the fields to insert into our sql database. One of the fields in the excel file has dates in the following format 4/13/2012. This value is supposed to go into a datetime field in sql server, yet no matter what i try I keep getting an error inserting it. What is the best way to go about taking a string such as 4/12/2012 and getting it into the database. Also will i need single quotes around the value in the query or not? Thanks for any help.
cmdInsert = New SqlCommand("INSERT INTO [empbill] ([deptcode], [personNo], [entrydate]) VALUES (@deptcode, @personNo, @entrydate);", db)
cmdInsert.Parameters.AddWi thValue("d eptcode", eno.Text)
cmdInsert.Parameters.AddWi thValue("p ersonNo", TextBox5.Text)
cmdInsert.Parameters.AddWi thValue("e ntrydate", dd.Text)
HTH
cmdInsert.Parameters.AddWi
cmdInsert.Parameters.AddWi
cmdInsert.Parameters.AddWi
HTH
ASKER
So I figured out what the problem is, the excel file we receive has the cells formatted as a date.. for whatever reason when my application reads in the value it wants to read in a double value. I went to the excel file and converted the cells to General and im now seeing where my program is pulling that double value from. 41085 is the format they are in. Is there a way to convert that to a date in C#??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
After hours of troulbe shooting and researching google I found the solution and it worked.
DbCommand cmd = db.GetSqlStringCommand(@"I
...
,[FirstDate]
...
) VALUES (@FirstDate,...");
db.AddInParameter(cmd, "@FirstDate", DbType.DateTime, DateTime.Now );
HTH
Ashok