在SQL Server数据库上执行简单查询时没有错误或结果 - c#

这是我使用SQL Server的第一种方法。我已经将Access DB导出到SQL Server,并想在我的应用程序中使用它。我已将新的SQL DB添加到C#项目中,并用OleDB替换了Sql。我现在无法执行在Access中与本地数据库完美配合的查询。

查询:

string query = @"SELECT SessionID, SemesterA, SemesterB, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
               " FROM [Session] " +
               " WHERE RoomID = @RoomID " +
               " AND SessionDate = getdate() ";

我已按照VS错误的指示将Date()替换为getdate(),但是查询不会产生任何结果(应返回一条记录,Access DB会返回)

我的RoomSelect表单代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoReg
{
    public partial class RoomSelect : Form
    {

        DataTable queryResult = new DataTable();
        public string RoomID;
        RoomActiveSession RoomActiveSessionForm = new RoomActiveSession();

        public RoomSelect()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            switch (listBox1.SelectedItem.ToString())
            {
                case "MB0302":
                    RoomID = listBox1.SelectedItem.ToString();
                    roomQuery();
                    break;

                case "MC1001":
                    RoomID = listBox1.SelectedItem.ToString();
                    roomQuery();
                    break;

                case "MC3203":
                    RoomID = listBox1.SelectedItem.ToString(); 
                    roomQuery();
                    break;

                case "MC3204":
                    RoomID = listBox1.SelectedItem.ToString();
                    roomQuery();
                    break;

            }
        }

        public void roomQuery()
        {
            string ConnStr = "Data Source=DUZY;Initial Catalog=AutoRegSQL;Integrated Security=True";

            SqlConnection MyConn = new SqlConnection(ConnStr);
            MyConn.Open();

            //SQL query that todays sessions for the given roomID
            string query = @"SELECT SessionID, SemesterA, SemesterB, RoomID, SessionDate, SessionTimeStart, SessionTimeEnd" +
               " FROM [Session] " +
               " WHERE RoomID = @RoomID " +
               " AND SessionDate = getdate() ";

            SqlCommand command = new SqlCommand(query, MyConn);

            command.Parameters.Add("RoomID", SqlDbType.Char).Value = RoomID;


            SqlDataAdapter adapter = new SqlDataAdapter(command);

            adapter.Fill(queryResult);

            if (queryResult.Rows.Count == 0)
            {
                MessageBox.Show("No active sessions today for the given room number");
                MyConn.Close();
            }
            else
            {

                RoomActiveSessionForm.SetDataSouce(queryResult);

                this.Hide();
                RoomActiveSessionForm.ShowDialog();

                MyConn.Close();
            }


        }

    }
}

当我运行该程序时,我收到一条消息“给定房间号今天没有活动的会话”,如果查询没有结果,则应执行该消息,但是我知道,它应该返回一条记录。

参考方案

函数getdate()实际上返回一个datetime。尝试将其转换为日期:

AND SessionDate = cast(getdate() as date)

时间部分可能是问题所在–阻止了日期和日期时间之间的匹配。

SQLAlchemy中的反射不适用于MS SQL Server系统表吗? - python

我试图在MS SQL Server数据库中反映系统表:from sqlalchemy import engine, create_engine, MetaData, Table meta = MetaData() url = engine.url.URL( "mssql+pyodbc", username=credentials[…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…