将日期格式保存到数据库

时间:2014-01-17 08:31:59

标签: vb.net csv datagridview

我想创建一个程序,在CSV中上传DataGridView文件,然后显示csv文件,我希望datagridview中的所有数据都保存在我的数据库中...问题是日期格式无法显示为yyyy/MM/dd到数据库。

此代码出错

No default member found for type 'Date'.

代码

Dim sched_date As String

    Dim agent_name, campaign_name, team_name, schedule As Object
    For i As Integer = 0 To DataGridView1.Rows.Count - 1

        agent_name = DataGridView1.Rows(i).Cells(0).Value()
        campaign_name = DataGridView1.Rows(i).Cells(1).Value()
        team_name = DataGridView1.Rows(i).Cells(2).Value()
        sched_date = DataGridView1.Rows(i).Cells(3).Value("yyyy/MM/dd")
        schedule = DataGridView1.Rows(i).Cells(4).Value()



        query = " Select * from tbl_sched WHERE   agent_name = '" + agent_name + "'"
        mysql_connect(True)
        cmd = New MySqlCommand(query, SQLConnection)
        reader = cmd.ExecuteReader()

        If reader.Read() Then
            mysql_connect(False)
            query = "update tbl_sched set campaign_name='" + campaign_name + "',team_name='" + team_name + "',sched_date='" + sched_date + "',schedule='" + schedule + "' WHERE agent_name ='" + agent_name + "'"


            mysql_connect(True)
            cmd = New MySqlCommand(query, SQLConnection)
            cmd.Connection = SQLConnection
            cmd.CommandText = query
            cmd.ExecuteNonQuery()


        Else
            mysql_connect(False)
            query = "insert into tbl_sched (agent_name,campaign_name,team_name,sched_date,schedule) values ('" + agent_name + "','" + campaign_name + "','" + team_name + "','" + sched_date + "','" + schedule + "')"
            mysql_connect(True)
            cmd = New MySqlCommand(query, SQLConnection)
            cmd.Connection = SQLConnection
            cmd.CommandText = query
            cmd.ExecuteNonQuery()

        End If

    Next
    MsgBox("Successfully upload CSV file to Database")

1 个答案:

答案 0 :(得分:0)

上面代码的修改可以是以下

Dim sched_date As DateTime
Dim agent_name, campaign_name, team_name, schedule As String

mysql_connect(True/False) ' I don't know what happens here '

For i As Integer = 0 To DataGridView1.Rows.Count - 1

    agent_name = DataGridView1.Rows(i).Cells(0).Value.ToString()
    campaign_name = DataGridView1.Rows(i).Cells(1).Value.ToString()
    team_name = DataGridView1.Rows(i).Cells(2).Value.ToString()
    sched_date = Convert.ToDateTime(DataGridView1.Rows(i).Cells(3).Value)
    schedule = DataGridView1.Rows(i).Cells(4).Value().ToString()

    query = "update tbl_sched set campaign_name=@cname,team_name=@tname,sched_date=@sdate," + 
             "schedule=@sched WHERE agent_name =@aname"

    cmd = New MySqlCommand(query, SQLConnection)
    cmd.Connection = SQLConnection
    cmd.CommandText = query
    cmd.Parameters.AddWithValue("@cname",campaign_name)
    cmd.Parameters.AddWithValue("@tname",team_name)
    cmd.Parameters.AddWithValue("@sdate",sched_date)
    cmd.Parameters.AddWithValue("@sched", schedule)
    cmd.Parameters.AddWithValue("@aname", agent_name)
    Dim rowsAffected = cmd.ExecuteNonQuery()

    ' If the Update doesn't find a record then rowsAffected is zero and we could execute the insertion here
    if rowsAffected = 0 Then
        query = "insert into tbl_sched " + 
                "(agent_name,campaign_name,team_name,sched_date,schedule) " + 
                "values (@aname,@cname,@tname,@sdate,@sched)"
        cmd.CommandText = query
        cmd.ExecuteNonQuery()
    End If

Next
MsgBox("Successfully upload CSV file to Database")

首先,我为变量使用了特定的数据类型。对于DateTime,我强制将Value网格字段转换为DateTime变量而不是字符串。

第二步是删除agent_name的查找。如果没有找到更新记录,那么简单地执行更新将返回零,因此您可以执行插入(根据已存在的代理数量,这可以节省大量往返数据库)。您还可以查看this question

中使用的语法INSERT INTO ... ON DUPLICATE

第三部分是删除字符串连接并用参数化查询替换它。通过这种方式,不可能进行sql注入,并且正确格式化基础数据库表的参数值的工作将传递给处理日期,字符串和小数的框架代码,并具有更好的知识。

不要忘记这种方法的优点也是更容易理解的编写sql字符串。

(我不知道其他数据库字段的数据类型是什么,如果需要,转换也应该应用于其他非字符串列)