0

datagridview からデータベースに datetime を挿入したいのですが、保存は必要ありません。

元。

Datagridview
DAte
2012-11-20 16:36:39

when i'm save to datagridview to datebase
DAte
1900-01-01 00:00:00.000

2012-11-20 16:36:39 を dataasbe に保存したいのですが、結果になりません。

データグリッドビューを表示

    Dim sqlSentMaterial As String = ""
    Dim DT_SentMaterial As New DataTable

    sqlSentMaterial = "SELECT rc.No,rc.ReceiveDate as RCReceiveDate,rc.CreateBy as RCReceiveBy,rc.CreateDate,rc.ReceiveNote "
    sqlSentMaterial &= "FROM RClother rc "
    sqlSentMaterial &= "Where rc.No ='" & Search & "'"
    DT_SentMaterial = FShowData(sqlSentMaterial)

   For i As Integer = 0 To DT_SentMaterial.Rows.Count - 1
        If DT_SentMaterial.Rows.Count <> 0 Then
            Dim item As New DataGridViewRow
            item.CreateCells(DgvSentMaterial)
            With item
                .Cells(1).Value = TxtBarcode.Text 
                If DT_SentMaterial.Rows(0).Item("RCReceiveDate") Is DBNull.Value Then
                    .Cells(2).Value = ""
                Else
                    .Cells(2).Value = CDate(DT_SentMaterial.Rows(0).Item("RCReceiveDate")).ToString("dd/MM/yyyy")
                End If
            End With
            DgvSentMaterial.Rows.Add(item)
            Exit For
        End If
    Next

データベースへのフォーム保存datagridview

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim sentMat As Integer

sentMat = FInsertSentMatClother(CInt(DgvSentMaterial.Rows(i).Cells(1).Value), CDate(DgvSentMaterial.Rows(i).Cells(2).Value))
End Sub

関数をデータベースに挿入

Public Function FInsert_iPOSentMatClother(ByVal BarCode As Integer, ByVal RCReceiveDate As Date)

        Dim Tr As SqlTransaction
        Dim sqlCom As New SqlCommand

        Dim sqlInsert As String
        Dim ReturnValue As Integer
        Dim GetDate As DateTime = DateTime.Now

        Try
            Tr = Conn.BeginTransaction
            sqlCom.Connection = Conn


            sqlInsert = "INSERT INTO SentMatClother "
            sqlInsert &= "(BarCode,ReceiveDate) "
            sqlInsert &= "VALUES('" & BarCode & "','" & RCReceiveDate & "')"

            sqlCom.Transaction = Tr
            sqlCom.CommandText = sqlInsert
            sqlCom.CommandType = CommandType.Text

            ReturnValue = CInt(sqlCom.ExecuteScalar) 'CInt(sqlCom.ExecuteScalar)
            If ReturnValue = 0 Then
                Tr.Commit()
            Else
                Tr.Rollback()
            End If
        Catch ex As Exception
            'MsgBox(ex.ToString)
        Finally
            If Not sqlCom Is Nothing Then
                sqlCom.Dispose()
            End If
            sqlCom = Nothing
        End Try
        Return ReturnValue

    End Function

お時間をいただきありがとうございます。:)

4

1 に答える 1

0

Function insert to database必要に応じて文字列の日付値をフォーマットします

sqlInsert = "INSERT INTO SentMatClother "
sqlInsert &= "(BarCode,ReceiveDate) "
sqlInsert &= "VALUES('" & BarCode & "','" & RCReceiveDate.ToString("yyyy-MM-dd HH:mm:ss") & "')"

または、私の意見では、パラメーターを使用することをお勧めします。

sqlInsert = "INSERT INTO SentMatClother (BarCode,ReceiveDate) VALUES (@barCode, @receiveDate)"

//then add parameters to your sqlCommand

sqlCom.Parameters.AddWithValue("@barCode", BarCode)
sqlCom.Parameters.AddWithValue("@barCode", RCReceiveDate)
于 2013-08-10T10:24:28.850 に答える