我在ASP.NET和SqlServer工作。
我的应用程序中有两个带日历扩展程序的文本框,用户将选择两个日期。我想从sqlserver获取这两个日期之间的数据,其中该特定字段的数据类型为 DateTime 即可。请告诉我如何继续这个...我写了一个查询,但那不起作用..
我的疑问:
SqlCommand cmd = new SqlCommand("select top 1 OrderNumber from tblOrderMaster where OrderedDate>='" + txtfromcal.Text + "' and OrderedDate<='" + txttocal.Text + "' ", conn);
答案 0 :(得分:7)
要做的事情
using
语句来正确处理对象try-catch
块来处理激励例如
string query = @"select top 1 OrderNumber
from tblOrderMaster
where OrderedDate BETWEEN @startDate AND @endDate";
using(SqlConnection conn = new SqlConnection("connectionString here"))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@startDate", txtfromcal.Text);
cmd.Parameters.AddWithValue("@endDate", txttocal.Text);
try
{
conn.Open();
// other codes
// to fetch the record
}
catch(SqlException e)
{
// do something with
// e.ToString()
}
}
}
SOURCES
答案 1 :(得分:1)
使用此代码:
Sqlcommand cmd=new sql command ("Select data from tablename
where date>=startdate
and date<=enddate",connection)
答案 2 :(得分:0)
试试这个
SELECT * FROM YourTableName WHERE sqlDateColumnName BETWEEN'“+ textbox1.Text +”'AND'“+ textbox2.Text +”'