使用VB.Net将当前时间戳插入postgresql

时间:2015-05-24 10:00:26

标签: vb.net postgresql-9.3

这是我在postgresql数据库中的插入函数:

Private Sub insertIntoDataBase(date As DateTime, value As String)

    Dim Command As NpgsqlCommand

    Command = New NpgsqlCommand("insert into test_base(date,value) values(" + date + "," + value ")", conn)
    Dim rowsaffected As Int32
    Try
        rowsaffected = Command.ExecuteNonQuery()
        Console.WriteLine("It was added {0} lines in table table1", rowsaffected)
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

End Sub

我想要做的是插入当前时间戳(在日期字段中),而不传递插入函数中的值。就像字段ID一样,自动插入"自动增量"。这可能吗?

1 个答案:

答案 0 :(得分:0)

我不知道postgressql(在mySql中它是可能的)但是在vb.net中,也许你可以这样改变你的代码:

Private Sub insertIntoDataBase(value As String)

Dim Command As NpgsqlCommand
Dim mTS as datetime = DateTime.Now

Command = New NpgsqlCommand("insert into test_base(date,value) values(" + mTS.tostring("yyyy-MM-dd HH:mm") + "," + value + ")", conn)
Dim rowsaffected As Int32
Try
    rowsaffected = Command.ExecuteNonQuery()
    Console.WriteLine("It was added {0} lines in table table1", rowsaffected)
Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try

End Sub

我使用了mySql的时间戳格式:根据需要进行更改。

注意:在你的代码中它缺少一个“+”