好的,所以我想做的是拿两个日期,减去它们并将它们的答案(整数)插入另一列。应该是15天等等。要获取的日期存储在数据库中。答案需要在DAYS 这是我的gridview代码:
<div class="gview">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" CssClass="gview" DataKeyNames="PID" Width="613px">
<Columns>
<asp:BoundField DataField="PID" HeaderText="PID" SortExpression="PID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Pname" HeaderText="Pname" SortExpression="Pname" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
<asp:BoundField DataField="Ward" HeaderText="Ward" SortExpression="Ward" />
<asp:BoundField DataField="Bed" HeaderText="Bed" SortExpression="Bed" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="AdmitDate" HeaderText="AdmitDate" SortExpression="AdmitDate" />
<asp:BoundField DataField="DischargeDate" HeaderText="DischargeDate" SortExpression="DischargeDate" />
<asp:BoundField DataField="NumOfDays" HeaderText="NumOfDays" SortExpression="NumOfDays" />
</Columns>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" SelectCommand="SELECT [PID], [Pname], [Gender], [Department], [Ward], [Bed], [Status], [AdmitDate], [DischargeDate], [NumOfDays] FROM [Patient]">
</asp:SqlDataSource>
</div>
这是我的C#代码:
protected void Button6_Click(object sender, EventArgs e)
{
string str = "update Patient (Department, Ward, Bed, Status, AdmitDate, DischargeDate) set Department= @Department,Ward=@Ward,Bed=@Bed,Status=@Status,AdmitDate=@AdmitDate,DischargeDate=@DischargeDate where PID = '" + TextBox3.Text + "'";
cmd = new SqlCommand(str, con);
con.Open();
cmd.Parameters.AddWithValue("@Department", DropDownList4.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Ward", DropDownList3.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Bed", DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Status", DropDownList2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@AdmitDate", TextBox1.Text);
DateTime.Parse(TextBox1.Text.ToString());
cmd.Parameters.AddWithValue("@DischargeDate", TextBox2.Text);
DateTime.Parse(TextBox2.Text.ToString());
var diff = DateTime.Parse(TextBox2.Text.ToString()).Subtract(DateTime.Parse(TextBox1.Text.ToString()));
cmd.Parameters.AddWithValue("@NumOfDays", diff);
cmd.ExecuteNonQuery();
con.Close();
show();
Response.Write("Record Updated");
}
现在,当我点击按钮时,它会产生以下错误:
SqlDbType.Time溢出。价值&#39; 10.00:00:00&#39;超出范围。必须在00:00:00.0000000和23:59:59.9999999之间。
我做了很多研究,发现了我已经实施的想法。但到目前为止它并不好。非常感谢任何形式的帮助。
答案 0 :(得分:3)
这些行:
var diff = DateTime.Parse(TextBox2.Text.ToString()).Subtract(DateTime.Parse(TextBox1.Text.ToString()));
cmd.Parameters.AddWithValue("@NumOfDays", diff);
将@NumOfDays
参数的值设置为TimeSpan
。首先,我建议明确设置您期望使用的数据类型 - 这样可以更容易地更早地找到错误。
接下来,您说您希望列的值为天数,但您没有在任何地方说过如何转换TimeSpan
。您可能希望使用TimeSpan.Days
,这假设您不关心任何子日信息。所以:
var diff = ...; // Code as before, but see notes.
cmd.Parameters.Add("@NumOfDays", SqlDbType.Int).Value = diff.Days;
但是,对于计算diff
:
TryParse
或TryParseExact
清楚地处理用户错误。ToString
的{{1}}属性上致电Text
- 它已经是一个字符串TextBox
运算符因此,如果你做想要使用文本框,那就像是:
-
接下来,查看您的SQL:
DateTime start, end;
if (!DateTime.TryParseExact(startTextBox.Text, "yyyy-MM-dd",
CultureInfo.InvariantCulture, DateTimeStyles.None,
out start))
{
// Invalid start - handle appropriately.
}
if (!DateTime.TryParseExact(endTextBox.Text, "yyyy-MM-dd",
CultureInfo.InvariantCulture, DateTimeStyles.None,
out end))
{
// Invalid end - handle appropriately.
}
var diff = end - start;
这有两个重要问题:
string str = "update Patient (Department, Ward, Bed, Status, AdmitDate, DischargeDate) set Department= @Department,Ward=@Ward,Bed=@Bed,Status=@Status,AdmitDate=@AdmitDate,DischargeDate=@DischargeDate where PID = '" + TextBox3.Text + "'";
直接包含在SQL中。 不要这样做。这是一个严重的潜在安全漏洞(SQL Injection Attack)。你正在使用其他所有参数 - 为什么没有PID?PID
参数或提及NumOfDays
列,因此显然不会受到影响。答案 1 :(得分:1)
你应该做这样的事情
DateTime admitdate = new DateTime(2015, 2, 17);
DateTime dischargedate = new DateTime(2015, 2, 19);
var diff = dischargedate.Subtract(admitdate).TotalDays;
// it will give you total days in this case it will give you 2 days
而不是
var diff = DateTime.Parse(TextBox2.Text.ToString()).Subtract(DateTime.Parse(TextBox1.Text.ToString()));
修改强>
DateTime admitdate = DateTime.Parse(TextBox1.Text);
DateTime dischargedate = DateTime.Parse(TextBox2.Text);
var diff = dischargedate.Subtract(admitdate).TotalDays;
答案 2 :(得分:0)
经过上述人员的所有研究和帮助,这对我有用:
DateTime AdmitDate = DateTime.Parse(TextBox1.Text);
cmd.Parameters.AddWithValue("@AdmitDate", AdmitDate.ToShortDateString());
DateTime DischargeDate = DateTime.Parse(TextBox2.Text);
cmd.Parameters.AddWithValue("@DischargeDate", DischargeDate.ToShortDateString());
var diff = (DischargeDate - AdmitDate).Days;
cmd.Parameters.AddWithValue("@NumOfDays", diff);
我必须使用ToShortDateString
,因为我不想将Time
位添加到AdmitDate
和DischargeDate
。由于我对这些列使用DateTime
数据类型,因此它会自动添加null
时间12:00AM
。 ToShortDateString
帮助我摆脱它。