English 中文(简体)
每天的问询
原标题:SQL query for datetime
  • 时间:2011-11-02 08:47:51
  •  标签:
  • c#
  • sql
string date = DateTime.Now.AddDays(day - 1).ToShortDateString().ToString();
string count = "select count(*) from Appointment_Info where  APPT_Dt= " +
               Convert.ToDateTime(date) + "   ";
SqlCommand cc = new SqlCommand(count, cn);
int appoinments = Convert.ToInt16( cc.ExecuteScalar().ToString());

以上问题看不出,是否说出了问题?

问题回答

下面是:

DateTime dtFrom = DateTime.Now.AddDays(day - 1).Date;
DateTime dtTo = past.AddDays(1);
string strSQL = "Select Count(*) From Appointment_Info Where  APPT_Dt Between @from And @to";
int appoinments = 0;
using (SqlCommand cc = new SqlCommand(strSQL, cn))
{
    cc.Parameters.AddWithValue("@from", dtFrom);
    cc.Parameters.AddWithValue("@to", dtTo);
    appoinments = Int32.Parse(cc.ExecuteScalar().ToString());
}

问题在于,当你赢得任何东西时,你就不需要exact<>strong>,而是需要range<>/strong> 日期,意思是过去和之后的一天。

上述法典还改进了使用参数和适当处置指挥物体的做法。

我认为,这将解决你的问题:

    string date = DateTime.Now.AddDays(day - 1).ToShortDateString().ToString();
    string count = "select count(*) from Appointment_Info where  convert(int,convert(varchar, APPT_Dt,112))=convert(int,convert(varchar, @date,112)) ";

    SqlCommand cc = new SqlCommand(count, cn);
    cc.parameters.AddWithValue("@date",date);
    int appoinments = Convert.ToInt16( cc.ExecuteScalar().ToString());

为此:

DateTime dt = DateTime.Now.AddDays(day - 1);
SqlCommand cc = new SqlCommand(
    "SELECT COUNT(*) FROM Appointment_Info WHERE APPT_Dt=@dt", cn);
cc.Parameters.AddWithValue("@dt", dt);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...