我有這樣的:日期時間VB網SQL表
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
從,prtscreen表的設計從SSM:
我試着去dlaser的DateTime.Now值域一個SQL字段。我得到SQL類型溢出,日期必須在等等之間等。
日期女巫短信顯示格式是:2016-09-30 14:58:46.343。 SQL Server 2005.
我怎樣才能確保vb net應用程序以相同的格式輸出datetime?
您提供的代碼實際上並不匹配您所說的內容。如果你需要一些不起作用的代碼的幫助,那麼提供實際的代碼將是一個很好的開始。您也可以提供實際的錯誤消息,而不是一些模糊的近似值。細節很重要。你試圖插入數據的列的數據類型是什麼? – jmcilhinney
如果您在.NET代碼和SQL Server中使用正確的數據類型(日期時間),則不需要關心顯示格式,因爲它只是_display_格式而不是數據存儲。你得到你的溢出異常的原因是,在流程中的某個地方,你不用正確的數據類型處理它,但可能是一個易受當前文化信息/語言環境影響的字符串 –
此外,你發佈的代碼使用'SqlDbType.Date ',它對應於SQL Server'date'數據類型。那只是日期,而不是時間。你使用'DateTime.Now'這個事實,你的格式化文本包含一個時間表明這不是你應該使用的。如果你想存儲日期和時間,那麼你應該在SQL Server中使用'datetime','datetime2'或'datetimeoffset'和ADO.NET代碼中相應的'SqlDbType'。 – jmcilhinney