我有 2 个文本框,txtStartDate.Text 和 txtEndDate.Text,用户通过日历日期选择器选择日期。现在我想计算两个选定日期之间的天数并将结果保存在数据库字段 totalDay(type integer) 中,以下是我的代码:
但是当我单击按钮并尝试将其保存到数据库中时,我收到此错误:
正在使用的 SQL Server 版本不支持数据类型“时间”。
我应该怎么做才能克服这个问题?
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Globalization
Partial Class addevent
Inherits System.Web.UI.Page
Protected Sub ButtonAddEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAddEvent.Click
Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
'"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eventdb.mdf;Integrated Security=True;User Instance=True"
Dim con As SqlConnection = New SqlConnection(connString)
Dim cmdQuery As String = "INSERT INTO eventinfo(venue,totalDay,eventTitle,startDate,endDate,description) VALUES (@venue,@totalDay,@eventTitle,@startDate,@endDate,@description)"
Dim cmd = New SqlCommand(cmdQuery)
cmd.Connection = con
cmd.CommandType = CommandType.Text
con.Open()
cmd.Parameters.AddWithValue("@eventTitle", txtEventTitle.Text)
cmd.Parameters.AddWithValue("@venue", txtEventLocation.Text)
cmd.Parameters.AddWithValue("@startDate", txtStartDate.Text)
cmd.Parameters.AddWithValue("@endDate", txtEndDate.Text)
cmd.Parameters.AddWithValue("@description", txtEventDescription.Text)
Dim fmt As String = "dd/MM/yyyy"
Dim dtStart As DateTime = DateTime.ParseExact(txtStartDate.Text, fmt, Nothing)
Dim dtEnd As DateTime = DateTime.ParseExact(txtEndDate.Text, fmt, Nothing)
Dim ts As TimeSpan = dtEnd - dtStart
cmd.Parameters.AddWithValue("@totalDay", ts.TotalDays)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
con.Close()
Response.Redirect("~/addbooth.aspx")
End Sub
End Class
我的表结构: