2

这是我第一次使用 SQL Server。我已将 Access DB 导出到 SQL Server,并希望在我的应用程序中使用它。我已将新的 SQL DB 添加到我的 C# 项目并替换OleDBSql. 我现在无法执行与 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();
            }


        }

    }
}

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

4

1 回答 1

6

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

AND SessionDate = cast(getdate() as date)

时间部分可能是问题——阻止日期和日期时间之间的匹配。

于 2013-08-07T14:47:12.110 回答