Datetime VB net到SQL表

时间:2016-10-14 05:58:47

标签: sql-server vb.net datetime

我有这个:

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim con As New SqlConnection
    Dim myCommand As New SqlCommand
    Try
        Dim a As String
        con.ConnectionString = "Data Source=SVNAV;Initial Catalog=NAV_Vermorel_Live;User ID=sa;Password=1234"
        con.Open()
        Dim laser As String
        Dim debit As String
        Dim indoire As String
        Dim uzinaj As String
        Dim dlaser As Nullable(Of DateTime) = DateTime.Now
        Dim ddebit As Nullable(Of DateTime) = DateTime.Now
        Dim dindoire As Nullable(Of DateTime) = DateTime.Now
        Dim duzinaj As Nullable(Of DateTime) = DateTime.Now
        If NewCheckBox1.Checked = True Then
            laser = "Finished"
        Else
            laser = "In Progress"
            dlaser = Nothing
        End If
        If NewCheckBox2.Checked = True Then
            debit = "Finished"
        Else
            debit = "In Progress"
            ddebit = Nothing
        End If
        If NewCheckBox3.Checked = True Then
            indoire = "Finished"
        Else
            indoire = "In Progress"
            dindoire = Nothing
        End If
        If NewCheckBox4.Checked = True Then
            uzinaj = "Finished"
        Else
            uzinaj = "In Progress"
            duzinaj = Nothing
        End If
        a = "INSERT INTO [dbo].[SC Vermorel SRL$PregatirePROD]
       (
        [FPO]
       ,[Articol]
       ,[Descriere]
       ,[Cantitate]
       ,[LASER]
       ,[DEBITARE]
       ,[INDOIRE]
       ,[UZINAJ]
       ,[EndDateLASER]
       ,[EndDateDEBIT]
       ,[EndDateINDOIRE]
       ,[EndDateUZINAJ])
 VALUES
       (@FPO,
        @Articol
       ,@Descriere
       ,@Cantitate
       ,@LASER
       ,@DEBITARE
       ,@INDOIRE
       ,@UZINAJ
       ,@EndDateLASER
       ,@EndDateDEBIT
       ,@EndDateINDOIRE
       ,@EndDateUZINAJ)"
        myCommand = New SqlCommand(a, con)
        myCommand.Parameters.AddWithValue("@FPO", txtFpo.Text)
        myCommand.Parameters.AddWithValue("@Articol", txtItem.Text)
        myCommand.Parameters.AddWithValue("@Descriere", txtDesc.Text)
        myCommand.Parameters.AddWithValue("@Cantitate", txtQty.Text)
        myCommand.Parameters.AddWithValue("@LASER", laser)
        myCommand.Parameters.AddWithValue("@DEBITARE", debit)
        myCommand.Parameters.AddWithValue("@INDOIRE", indoire)
        myCommand.Parameters.AddWithValue("@UZINAJ", uzinaj)
        myCommand.Parameters.AddWithValue("@EndDateLaser", dlaser)
        myCommand.Parameters.AddWithValue("@EndDateDEBIT", ddebit)
        myCommand.Parameters.AddWithValue("@EndDateINDOIRE", dindoire)
        myCommand.Parameters.AddWithValue("@EndDateUZINAJ", duzinaj)
        myCommand.ExecuteNonQuery()
    Catch ex As Exception
        MessageBox.Show("Eroare ..." & ex.Message, "Inserare campuri")
    Finally
        con.Close()
    End Try
    Me.SC_Vermorel_SRL_PregatirePRODTableAdapter.Fill(Me.NAV_Vermorel_LiveDataSet._SC_Vermorel_SRL_PregatirePROD)
End Sub

桌面设计来自SSM的prtscreen:

tabledesign

我试图将dlaser的DateTime.Now值添加到SQL字段中。我得到SQL类型溢出,日期必须介于等等之间等。

SMS显示的日期格式为:2016-09-30 14:58:46.343。 SQL Server 2005.

我怎样才能确定vb net应用程序以相同格式输出datetime?

2 个答案:

答案 0 :(得分:1)

WebViewControl.NavigationCompleted += WebViewControl_NavigationCompleted; private void WebViewControl_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) { System.Diagnostics.Debug.WriteLine("Actual height of webview:" + WebViewControl.ActualHeight); } 部分中,您将VB变量Else保留为未初始化,这意味着它具有值dlaser。但该变量始终用于参数0001-01-01 00:00:00以设置列[EndDateLASER]。

列[EndDateLASER]的SQL类型为@EndDateLaser,但datetime不允许datetime,允许的最小值为0001-01-01

除此之外,我想知道为什么你有时添加1753-01-01 SqlParameter(值为DBNull)。对于所示的查询,参数无关紧要,因为它不会在任何地方使用@dlaser。而且,为什么只在一种情况下添加它,而你的查询是固定的。

答案 1 :(得分:0)

请使用此

  

DateTime.Now.ToString(" DD-MMM-YYYY&#34)

代替

  

DateTime.Now

和dlaser是字符串

  

dim dlaser as string