Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Problem importing Date Field into SQL DB.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Hodgson - 29 Oct 2004 20:45 GMT
I'm trying to import from a text file into a Sql DB, but I'm getting hung up
on a date field.

My text file has the date in the dd/mm/yyyy format.
I Cast that field to CDate in VB.NET, which sets it to #12/31/2005#

On my Insert, I get a SQL error from the .NET SqlClient Data provider:
"Syntax Error converting datetime from character string"

I tried not casting it as Date, which passed it into the parameter as
"2005-12-31" but that gave me the same error.

Does anyone have any ideas?
Rulin Hong - 29 Oct 2004 22:25 GMT
Basically. If you use CDate, The sting should meet
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern. Otherwise you should use Convert.ToDateTime(String, IFormatProvider).

If you use string to insert date column. The string should persist with SQL
server datatime format set from control panel-->Regional Options

> I'm trying to import from a text file into a Sql DB, but I'm getting hung up
> on a date field.
[quoted text clipped - 9 lines]
>
> Does anyone have any ideas?
Cor Ligthert - 30 Oct 2004 09:37 GMT
Kevin,

I made a sample for you, maybe you can try if it does the job for you.

\\\
Public Class Main
   Public Shared Sub Main()
       Dim Conn As New SqlClient.SqlConnection _
       ("Server=localhost;DataBase=Northwind;Integrated Security=SSPI")
       Try
           Threading.Thread.CurrentThread.CurrentCulture = _
                            New Globalization.CultureInfo("en-US")
           'The above only because my system setting is nl-NL

           Dim strSQL As String = "INSERT INTO Employees " & _
           "(LastName, FirstName, HireDate)" & _
           "VALUES ('Kevin', 'Hodgson',  @HireDate)"
           Dim cmd As New SqlClient.SqlCommand(strSQL, Conn)
           Dim myparam As New SqlClient.SqlParameter
           myparam.DbType = DbType.DateTime
           myparam.ParameterName = "@HireDate"
           myparam.Value = CDate("12-31-2005")
           cmd.Parameters.Add(myparam)
           Conn.Open()
           cmd.ExecuteNonQuery()
       Catch ex As SqlClient.SqlException
           MessageBox.Show(Ex.ToString)
       Catch ex As Exception
           MessageBox.Show(Ex.ToString)
       End Try
   End Sub
End Class
///

I hope it helps?

Cor
"Kevin Hodgson" <kevin@caseware.com>

> I'm trying to import from a text file into a Sql DB, but I'm getting hung
> up
[quoted text clipped - 10 lines]
>
> Does anyone have any ideas?
Cor Ligthert - 30 Oct 2004 09:46 GMT
Kevin,

Extra,  that setting of the globalization you should try to avoid, it is
only for this sample because I assume because of your messagedate you are in
the US.

Normally better would be this in the sample.
myparam.Value = New Date(2005, 12, 31)

Which goes in all globalization settings, however is a very academical
sample. Normally the user will enter the values in the globalization he is
used and is than the setting of globalization not needed because than
converting using CDate is done by the framework.

Cor

> Kevin,
>
[quoted text clipped - 49 lines]
>>
>> Does anyone have any ideas?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.