【问题标题】:ASP.NET/VB date works on localhost but not remoteserverASP.NET/VB 日期适用于 localhost 但不适用于远程服务器
【发布时间】:2017-07-16 10:35:40
【问题描述】:

我在尝试从 DateTime 选择器中获取日期以保存到我的数据库时遇到了很多麻烦。从 Localhost 运行时它工作正常,但在公共服务器上运行时它不会。 最初我使用了 cDate 函数,还尝试了 Convert.ToDateTime。但在服务器上,它现在给了我一个错误“字符串未被识别为有效的日期时间。”

我研究发现最好使用 DateTime.TryParse 函数。找到一个示例后,我为我的项目更改了它,现在我收到错误“必须声明标量变量“@articledate”。”

当我调试项目时,添加到“result1”DateTime 中的值是正确的。但它仍然在 ExecuteNonQuery 处抛出错误?

如果有区别的话,主服务器是由 GoDaddy 托管的,Localhost 是我的 PC。

任何帮助或建议将不胜感激。

    cnSQL1 = New SqlConnection(MSSQL.cRemoteConnectionString)
    cnSQL1.Open()
    sSQL = "SELECT NewsID FROM News WHERE Season = @season AND Heading = @heading AND ArticleDate = @articledate AND Author = @author AND Club = @club AND State = @state AND AddedDate = @addeddate AND AddedBy = @addedby AND Release = @release"
    Dim com1 As New SqlCommand(sSQL)
    com1.Connection = cnSQL1
    com1.Parameters.AddWithValue("@season", General.sSeason)
    com1.Parameters.AddWithValue("@heading", General.UppercaseFirstLetter(txtHeading.Text))
    Dim result1 As DateTime
    com.Parameters.AddWithValue("@articledate", DateTime.TryParseExact(txtArticleDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, result1))
    'com1.Parameters.AddWithValue("@articledate", Convert.ToDateTime(txtArticleDate.Text))
    com1.Parameters.AddWithValue("@author", txtAuthor.Text)
    com1.Parameters.AddWithValue("@club", ddlClub.SelectedItem.Value)
    com1.Parameters.AddWithValue("@state", dsState.Tables(0).Rows(0).Item(0))
    com1.Parameters.AddWithValue("@addeddate", Date.Today)
    com1.Parameters.AddWithValue("@addedby", Session("UserID"))
    com1.Parameters.AddWithValue("@updatedate", Date.Today)
    com1.Parameters.AddWithValue("@updateby", Session("UserID"))
    com1.Parameters.AddWithValue("@release", s)
    com1.ExecuteNonQuery()

【问题讨论】:

  • 设置日期选择器时,指定格式是一种安全、明确的格式,不受区域或语言设置的影响。 yyyymmdd 是安全的(yyyy-mm-dd 不是,m/d/yd/m/y 也不是)。

标签: asp.net sql-server vb.net datetime


【解决方案1】:

错误“必须声明标量变量“@articledate” 是由拼写错误引起的。添加参数时,应使用名为 com1 的 SqlCommand 变量,而不是变量com

但是在这之后你有一个更严重的错误。所有TryParse 方法都返回布尔值。转换后的日期写入传递给 TryParse 的最后一个参数中。

通过这种方式,您将布尔值传递给AddWithValue,并且此方法采用您在 value 参数中输入的任何内容。它无法知道你想要约会,所以它很乐意满足你的要求。

您应该这样做(注意要使用的 SqlCommand 是 com1 而不是 com

Dim result1 As DateTime
if DateTime.TryParseExact(txtArticleDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, result1) Then
    com1.Parameters.AddWithValue("@articledate", resul1)
Else
    .....

一般来说,avoid AddWithValue 会更好,因为它会接受你传递给它的任何东西。首选方式是使用

if DateTime.TryParseExact(txtArticleDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, result1) Then
    com1.Parameters.Add("@articledate", SqlDbType.Datetime).Value = result1
    com1.Parameters.AddWithValue("@season", General.sSeason)
    com1.Parameters.AddWithValue("@heading", General.UppercaseFirstLetter(txtHeading.Text))

    com1.Parameters.AddWithValue("@author", txtAuthor.Text)
    com1.Parameters.AddWithValue("@club", ddlClub.SelectedItem.Value)
    com1.Parameters.AddWithValue("@state", dsState.Tables(0).Rows(0).Item(0))
    com1.Parameters.AddWithValue("@addeddate", Date.Today)
    com1.Parameters.AddWithValue("@addedby", Session("UserID"))
    ' This is not used by the query, commented out
    ' com1.Parameters.AddWithValue("@updatedate", Date.Today)
    ' This is not used by the query, commented out
    ' com1.Parameters.AddWithValue("@updateby", Session("UserID"))
    com1.Parameters.AddWithValue("@release", s)
    com1.ExecuteNonQuery()
Else
    MessageBox.Show("Date is not valid")
End If

我还删除了上面查询中没有使用的两个参数(尽管它们不应该导致上面的错误消息)

【讨论】:

  • 感谢添加参数的提示。不幸的是,我仍然对日期有疑问。当它到达 If 语句时,它返回 false。所以跳过参数的添加并抛出标量异常。声明时或运行 If 语句时是否设置了“result1”?我已将上述代码(第二个答案)复制到我的项目中,但仍然抛出错误
  • 现在确切的错误信息是什么?考虑到我已经放了一个 if 来检查输入日期是否正确。如果不正确,则不应执行查询。我将扩展第二个示例以更好地解释流程
  • 调试时,'If DateTime.TryParseExact(txtArticleDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, result1) Then' 将返回 False。因此,当涉及到执行查询时,它会显示与上面相同的“必须声明标量变量“@articledate””,如果有帮助的话。 txtArticledate,文本显示“28/01/2017”,结果 1 显示“#12:00:00 AM#”
  • 将格式掩码更改为 dd/MM/yyyy 并使用您的 CultureInfo.CurrentCulture(但如果您指定正确的日期分隔符,它可能也适用于 InvariantCulture)或更好遵循 Aaron Bertrand 关于使用正确格式化的 DateTimePicker 而不是 TextBox 的合理建议。你不再需要解析代码
  • 我接受了建议并取消了文本框,只使用了 DateTimePicker。没有我希望的那么好,但它可以完成这项工作而不会过度复杂化。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 2020-09-03
  • 2012-01-01
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 2017-04-18
  • 2014-04-19
  • 2019-09-09
相关资源
最近更新 更多