我有一个数据库,存储彩票表单提交的用户。其中一列是DateTime列,每次有人提交表单时,都会用当前DateTime填充该列。我希望能够按DateTime列进行搜索,这样我就可以拉出在特定日期输入的所有表单。
我尝试禁用jquery datepicker,并尝试从数据库中复制并粘贴确切的DateTime字段到搜索框中,但仍然一无所获。很明显我漏掉了什么,但我搞不清楚是什么。任何帮助都是非常感谢的。
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class adminDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lastNameButton(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE Last LIKE '%'+ @Name + '%'", conn);
comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar, 50).Value = nameSearch.Text;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void groupNameButton(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE GroupName LIKE '%'+ @GroupName + '%'", conn);
comm.Parameters.Add("@GroupName", System.Data.SqlDbType.NVarChar, 50).Value = groupSearch.Text;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void dateSearch(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE dateTime = @dateTime ", conn);
comm.Parameters.Add("@dateTime", System.Data.SqlDbType.DateTime).Value = DateTime.Parse(datesearch.Text);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
BindLottoDetails();
}
private void BindLottoDetails()
{
int selectedRowIndex = grid.SelectedIndex;
int lottoId = (int)grid.DataKeys[selectedRowIndex].Value;
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT Id, First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, LotteryChoices, dateTime FROM Lotto WHERE Id=@Id", conn);
comm.Parameters.Add("Id", System.Data.SqlDbType.Int);
comm.Parameters["Id"].Value = lottoId;
try
{
conn.Open();
reader = comm.ExecuteReader();
lottoFormDetails.DataSource = null;
lottoFormDetails.DataSource = reader;
lottoFormDetails.DataKeyNames = new string[] { "Id" };
lottoFormDetails.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
protected void lottoFormDetails_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
lottoFormDetails.ChangeMode(e.NewMode);
BindLottoDetails();
}
protected void lottoFormDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int LottoId = (int)lottoFormDetails.DataKey.Value;
TextBox newFirstTextBox = (TextBox)lottoFormDetails.FindControl("editFirstTextBox");
TextBox newLastTextBox = (TextBox)lottoFormDetails.FindControl("editLastTextBox");
TextBox newAddr1TextBox = (TextBox)lottoFormDetails.FindControl("editAddr1TextBox");
TextBox newCityTextBox = (TextBox)lottoFormDetails.FindControl("editCityTextBox");
TextBox newStateTextBox = (TextBox)lottoFormDetails.FindControl("editStateTextBox");
TextBox newZipTextBox = (TextBox)lottoFormDetails.FindControl("editZipTextBox");
TextBox newdaytimephoneTextBox = (TextBox)lottoFormDetails.FindControl("editdaytimephoneTextBox");
TextBox neweveningphoneTextBox = (TextBox)lottoFormDetails.FindControl("editeveningphoneTextBox");
TextBox newemailTextBox = (TextBox)lottoFormDetails.FindControl("editemailTextBox");
TextBox newmembershipTextBox = (TextBox)lottoFormDetails.FindControl("editmembershipTextBox");
TextBox newhutCreditTextBox = (TextBox)lottoFormDetails.FindControl("edithutCreditTextBox");
TextBox newcreditNameTextBox = (TextBox)lottoFormDetails.FindControl("editcreditNameTextBox");
TextBox newGroupNameBox = (TextBox)lottoFormDetails.FindControl("editGroupNameTextBox");
TextBox newLotteryChoicesTextBox = (TextBox)lottoFormDetails.FindControl("editLotteryChoicesTextBox");
string newFirst = newFirstTextBox.Text;
string newLast = newLastTextBox.Text;
string newAddr1 = newAddr1TextBox.Text;
string newCity = newCityTextBox.Text;
string newState = newStateTextBox.Text;
string newZip = newZipTextBox.Text;
string newdaytimephone = newdaytimephoneTextBox.Text;
string neweveningphone = neweveningphoneTextBox.Text;
string newemail = newemailTextBox.Text;
string newmembership = newmembershipTextBox.Text;
string newhutCredit = newhutCreditTextBox.Text;
string newcreditName = newcreditNameTextBox.Text;
string newGroupName = newGroupNameBox.Text;
string newLotteryChoices = newLotteryChoicesTextBox.Text;
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("UpdateLotteryForm", conn);
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("Id", System.Data.SqlDbType.Int);
comm.Parameters["Id"].Value = LottoId;
comm.Parameters.Add("NewFirst", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewFirst"].Value = newFirst;
comm.Parameters.Add("NewLast", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewLast"].Value = newLast;
comm.Parameters.Add("NewAddr1", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewAddr1"].Value = newAddr1;
comm.Parameters.Add("NewC", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewC"].Value = newCity;
comm.Parameters.Add("NewS", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewS"].Value = newState;
comm.Parameters.Add("NewZ", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewZ"].Value = newZip;
comm.Parameters.Add("Newdayphone", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newdayphone"].Value = newdaytimephone;
comm.Parameters.Add("Neweveningphone", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Neweveningphone"].Value = neweveningphone;
comm.Parameters.Add("Newemail", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newemail"].Value = newemail;
comm.Parameters.Add("Newmembership", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newmembership"].Value = newmembership;
comm.Parameters.Add("NewhutCredit", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewhutCredit"].Value = newhutCredit;
comm.Parameters.Add("NewcreditName", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewcreditName"].Value = newcreditName;
comm.Parameters.Add("NewGroupName", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewGroupName"].Value = newGroupName;
comm.Parameters.Add("NewLotteryChoices", System.Data.SqlDbType.NVarChar, -1);
comm.Parameters["NewLotteryChoices"].Value = newLotteryChoices;
try
{
conn.Open();
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
lottoFormDetails.ChangeMode(DetailsViewMode.ReadOnly);
BindLottoDetails();
}
}.aspx代码
<%@ Page Title="" Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="adminDefault.aspx.cs" Theme="Blue" Inherits="adminDefault" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" ></script>
<script>
$(function () {
$("#<%=datesearch.ClientID%>").datepicker();
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="container">
<div class="row">
<div class="col-md-12">
<h2>10th Mountain Lottery App</h2>
</div>
</div>
<div class="row">
<div class="col-md-2 sidenav">
<ul>
<li>Search Forms</li>
<li>Display Full Lottery List</li>
<li>Error Filtering</li>
</ul>
</div>
<div class="col-md-10">
<p>You can use the menu below to search for individual forms</p>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h4>Lottery Form Search</h4>
<asp:Label runat="server">Search by Last Name: <asp:TextBox id="nameSearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="nameSearchButton" runat="server" Text="Search" OnClick="lastNameButton" /></p>
<asp:Label runat="server">Search by Group Name: <asp:TextBox id="groupSearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="groupsearchButton" runat="server" Text="Search" OnClick="groupNameButton" /></p>
<asp:Label runat="server">Search by Date: <asp:TextBox id="datesearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="dateSearchButton" runat="server" Text="Search" OnClick="dateSearch" /></p>
<asp:Label ID="noMatchLabel" runat="server"></asp:Label>
<asp:GridView id="grid" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="grid_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="First" HeaderText="First Name" />
<asp:BoundField DataField="Last" HeaderText="Last Name" />
<asp:BoundField DataField="C" HeaderText="City" />
<asp:BoundField DataField="GroupName" HeaderText="Group Name" />
<asp:ButtonField CommandName="Select" Text="Select" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView ID="lottoFormDetails" runat="server" AutoGenerateRows="False" OnItemUpdating="lottoFormDetails_ItemUpdating" OnModeChanging="lottoFormDetails_ModeChanging">
<Fields>
<asp:TemplateField HeaderText="First Name">
<EditItemTemplate>
<asp:TextBox ID="editFirstTextBox" runat="server" Text='<%# Bind("First") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertFirstTextBox" runat="server" Text='<%# Bind("First") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="FirstLabel" runat="server" Text='<%# Bind("First") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<EditItemTemplate>
<asp:TextBox ID="editLastTextBox" runat="server" Text='<%# Bind("Last") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertLastTextBox" runat="server" Text='<%# Bind("Last") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="LastLabel" runat="server" Text='<%# Bind("Last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<EditItemTemplate>
<asp:TextBox ID="editAddr1TextBox" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertAddr1TextBox" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Addr1Label" runat="server" Text='<%# Bind("Addr1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="editCityTextBox" runat="server" Text='<%# Bind("C") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertCityTextBox" runat="server" Text='<%# Bind("C") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="CityLabel" runat="server" Text='<%# Bind("C") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<EditItemTemplate>
<asp:TextBox ID="editStateTextBox" runat="server" Text='<%# Bind("S") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertStateTextBox" runat="server" Text='<%# Bind("S") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="StateLabel" runat="server" Text='<%# Bind("S") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip">
<EditItemTemplate>
<asp:TextBox ID="editZipTextBox" runat="server" Text='<%# Bind("Z") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertZipTextBox" runat="server" Text='<%# Bind("Z") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="ZipLabel" runat="server" Text='<%# Bind("Z") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Daytime Phone #">
<EditItemTemplate>
<asp:TextBox ID="editdaytimephoneTextBox" runat="server" Text='<%# Bind("dayphone") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertdaytimephoneTextBox" runat="server" Text='<%# Bind("dayphone") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="daytimephoneLabel" runat="server" Text='<%# Bind("dayphone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Evening Phone #">
<EditItemTemplate>
<asp:TextBox ID="editeveningphoneTextBox" runat="server" Text='<%# Bind("eveningphone") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="inserteveningphoneTextBox" runat="server" Text='<%# Bind("eveningphone") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="eveningphoneLabel" runat="server" Text='<%# Bind("eveningphone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<EditItemTemplate>
<asp:TextBox ID="editemailTextBox" runat="server" Text='<%# Bind("email") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertemailTextBox" runat="server" Text='<%# Bind("email") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="emailLabel" runat="server" Text='<%# Bind("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Membership">
<EditItemTemplate>
<asp:TextBox ID="editmembershipTextBox" runat="server" Text='<%# Bind("membership") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertmembershipTextBox" runat="server" Text='<%# Bind("membership") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="membershipLabel" runat="server" Text='<%# Bind("membership") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hut Credit">
<EditItemTemplate>
<asp:TextBox ID="edithutCreditTextBox" runat="server" Text='<%# Bind("hutCredit") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="inserthutCreditTextBox" runat="server" Text='<%# Bind("hutCredit") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="hutCreditLabel" runat="server" Text='<%# Bind("hutCredit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hut Credit Name">
<EditItemTemplate>
<asp:TextBox ID="editcreditNameTextBox" runat="server" Text='<%# Bind("creditName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertcreditNameTextBox" runat="server" Text='<%# Bind("creditName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="creditNameLabel" runat="server" Text='<%# Bind("creditName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Name">
<EditItemTemplate>
<asp:TextBox ID="editGroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertGroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%# Bind("GroupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lottery Choices">
<EditItemTemplate>
<asp:TextBox ID="editLotteryChoicesTextBox" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertLotteryChoicesTextBox" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="LotteryChoicesLabel" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
<HeaderTemplate>
<%#Eval("First")%> <%#Eval("Last") %> <%#Eval("dateTime") %>
</HeaderTemplate>
</asp:DetailsView>
</div>
</div>
</div>
</div>
</div>
</asp:Content发布于 2016-06-01 00:55:26
在SQL中,您声明将数据存储为DateTime。这意味着您将获得日期和时间。当然,时间包括小时、分钟、秒等。
在查询中,您需要将DateTime转换为日期,以便从中删除时间。
有关更多说明https://msdn.microsoft.com/en-us/library/ms187819.aspx,请参阅此MSDN文章
https://stackoverflow.com/questions/37550343
复制相似问题