使用VB.Net将日期从mm-dd-yyyy转换为MySql格式

时间:2014-05-28 05:58:20

标签: mysql vb.net

大家好日子。

这是我的另一个问题。由于第一个问题已由Tim Schmelter爵士解决。我会问另一组问题。感谢那些愿意帮助的人

我有两个问题:

我现在的第一个问题是关于约会。如何从文本文件转换日期,以便将其插入MySQL数据库?它在文本文件中的原始形式是(0246,04 / 16 / 2013,01:00,O),我不知道如何转换它

我的第二个问题是我确实将数据库中列的值更改为其正确的形式(日期到日期,id到int),但每当我尝试在' MySqlDbType中更改它们时。键入它表示'索引超出了数组的范围。并且错误指向代码' cmd.ExecuteNonQuery()'。我从' MySqlDbType.VarChar'更改了它。到' MySqlDbType.Date'等等,当我把Int或Integer放入其中时说它不是MySqlDbType的一部分。

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim filename As String = "C:\Users\user\desktop\trial.txt"
        Dim query As String = "INSERT INTO swipe_table VALUES (@Emp_id, @Date, @Time)"

        Dim allLines As String() = File.ReadAllLines(filename)

        Using con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=cph;User ID=root;Password=;")
            con.Open()
            Using cmd As New MySql.Data.MySqlClient.MySqlCommand(query, con)
                For Each line In allLines
                    Dim emp_id, dt, time As String
                    emp_id = line.Substring(0, 4)
                    dt = line.Substring(5, 10)
                    time = line.Substring(16, 4)
                    cmd.Parameters.Clear()
                    Dim pEmp_id As New MySql.Data.MySqlClient.MySqlParameter("@Emp_id", MySqlDbType.VarChar)
                    pEmp_id.Value = emp_id
                    cmd.Parameters.Add(pEmp_id)
                    Dim pDate As New MySql.Data.MySqlClient.MySqlParameter("@Date", MySqlDbType.VarChar)
                    pDate.Value = dt
                    cmd.Parameters.Add(pDate)
                    Dim pTime As New MySql.Data.MySqlClient.MySqlParameter("@Time", MySqlDbType.VarChar)
                    pTime.Value = time
                    cmd.Parameters.Add(pTime)
                    cmd.ExecuteNonQuery()
                Next
                MsgBox("All records were inserted successfully")
                con.Close()
            End Using
        End Using
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

如果您在数据库中存储了真实日期,那么无论数据库如何,您都需要在VB.NET中将文本转换为DateTime值。然后,您的ADO.NET提供商将担心其余部分。要将日期的String表示转换为DateTime,您可以使用ParseTryParseParseExactTryParseExact方法之一DateTime类型。

顺便说一下,你的日期显然是MM / dd / yyyy格式,而不是mm-dd-yyyy。

答案 1 :(得分:0)

我已经知道了,我刚刚添加了DateTime.ParseExact

Imports MySql.Data.MySqlClient
Imports System.IO
Imports System.Globalization

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles `enter     MyBase.Load
    Dim filename As String = "C:\Users\user\desktop\trial.txt"
    Dim query As String = "INSERT INTO swipe_table VALUES (@Emp_id, @Date, @Time)"

    Dim allLines As String() = File.ReadAllLines(filename)

    Using con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=cph;User ID=root;Password=;")
        con.Open()
        Using cmd As New MySql.Data.MySqlClient.MySqlCommand(query, con)
            For Each line In allLines
                Dim emp_id, time As String
                Dim dt As Date
                dt = DateTime.ParseExact("2012-05-20", "yyyy-MM-dd", CultureInfo.InvariantCulture)

                emp_id = line.Substring(0, 4)
                dt = line.Substring(5, 10)
                time = line.Substring(16, 5)
                cmd.Parameters.Clear()
                Dim pEmp_id As New MySql.Data.MySqlClient.MySqlParameter("@Emp_id", MySqlDbType.VarChar)
                pEmp_id.Value = emp_id
                cmd.Parameters.Add(pEmp_id)
                Dim pDate As New MySql.Data.MySqlClient.MySqlParameter("@Date", MySqlDbType.Date)
                pDate.Value = dt
                cmd.Parameters.Add(pDate)
                Dim pTime As New MySql.Data.MySqlClient.MySqlParameter("@Time", MySqlDbType.VarChar)
                pTime.Value = time
                cmd.Parameters.Add(pTime)
                cmd.ExecuteNonQuery()
            Next
            MsgBox("All records were inserted successfully")
            con.Close()
        End Using
    End Using
    End Sub
    End Class