我试图将日期(仅)从dateTimePicker插入到mysql数据库,但是发生了错误
我使用的格式
DateTimePicker1.Value.Date.ToString("yyyy/MM/dd")
在我的mysql数据库中,我使用DATE
数据类型
当我尝试在vb中显示日期时显示
正确显示日期格式yyyy/MM/dd
,但仍然无法将其插入我的数据库。
修改 我的代码插入数据库
Dim namaBarang, satuan, sql As String
Dim idtoko = formStok.pIdToko
Dim tglTransaksi = DateTimePicker1.Value.Date.ToString("yyyy/MM/dd")
Dim qty, idBarang As Integer
Dim noTrans = Label2.Text
strcon.Open()
For x As Integer = 0 To dataListTransaksi.Rows.Count - 1 Step 1
namaBarang = dataListTransaksi.Rows(x).Cells(2).Value.ToString
idBarang = dataListTransaksi.Rows(x).Cells(1).Value.ToString
satuan = dataListTransaksi.Rows(x).Cells(3).Value.ToString
qty = dataListTransaksi.Rows(x).Cells(4).Value.ToString
sql = "BEGIN;
INSERT
INTO
`tbl_transaksi`(
`tbl_transaksi`.`no_transaksi`,
`tbl_transaksi`.`tgl_transaksi`,
`tbl_transaksi`.`id_toko`
)
VALUES(" & noTrans & ", " & tglTransaksi & ", " & idtoko & ");
INSERT
INTO
`tbl_transaksi_detail`(
`tbl_transaksi_detail`.`no_transaksi`,
`tbl_transaksi_detail`.`id_barang`,
`tbl_transaksi_detail`.`qty`,
`tbl_transaksi_detail`.`unit`
)
VALUES(" & noTrans & ", " & idBarang & ", " & qty & ", '" & satuan & "');
COMMIT;"
Try
'HOLDS THE DATA TO BE EXECUTED
With cmd
.Connection = strcon
.CommandText = sql
'EXECUTE THE DATA
result = cmd.ExecuteNonQuery
'CHECKING IF THE DATA HAS EXECUTED OR NOT AND THEN THE POP UP MESSAGE WILL APPEAR
If result = 0 Then
MsgBox("FAILED TO SAVE THE DATA", MsgBoxStyle.Information)
Else
MsgBox("DATA HAS BEEN SAVE IN THE DATABASE")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
strcon.Close()
答案 0 :(得分:0)
我忘了在日期值附近添加单引号
sql = "BEGIN;
INSERT
INTO
`tbl_transaksi`(
`tbl_transaksi`.`no_transaksi`,
`tbl_transaksi`.`tgl_transaksi`,
`tbl_transaksi`.`id_toko`
)
VALUES(" & noTrans & ", " & tglTransaksi & "<<==THE PROBLEM IS HERE, " & idtoko & ");
INSERT
INTO
`tbl_transaksi_detail`(
`tbl_transaksi_detail`.`no_transaksi`,
`tbl_transaksi_detail`.`id_barang`,
`tbl_transaksi_detail`.`qty`,
`tbl_transaksi_detail`.`unit`
)
VALUES(" & noTrans & ", " & idBarang & ", " & qty & ", '" & satuan & "');
COMMIT;"
所以我改变它
VALUES(" & noTrans & ", '" & tglTransaksi & "', " & idtoko & ");
解决!