简体   繁体   English

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

[英]No errors or results when executing simple query on SQL Server database

This is my first approach to SQL Server. 这是我使用SQL Server的第一种方法。 I have exported my Access DB to SQL Server and want to use it in my application. 我已经将Access DB导出到SQL Server,并想在我的应用程序中使用它。 I have added the new SQL DB to my C# project and replaced OleDB with Sql . 我已经将新的SQL DB添加到我的C#项目中,并用Sql替换了OleDB I am now unable to execute queries which where perfectly working with local DB in Access. 我现在无法执行在Access中与本地数据库完美配合的查询。

Query: 查询:

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

I have replaced Date() with getdate() as instructed by the VS error, but the query does not produce any result (should return one record, Access DB does) 我已按照VS错误的指示,用getdate()替换了Date() ,但查询未产生任何结果(应该返回一条记录,Access DB会返回)

My RoomSelect form code: 我的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();
            }


        }

    }
}

When I run the program, I receive a message "No active sessions today for the given room number" which should be executed when there are no results to the query, but I know for a fact, that it should return one record) 当我运行该程序时,我收到一条消息“给定房间号今天没有活动的会话”,如果查询没有结果,则应执行该消息,但事实上,我知道它应该返回一个记录)

The function getdate() actually returns a datetime . 函数getdate()实际上返回一个datetime Try converting it to a date: 尝试将其转换为日期:

AND SessionDate = cast(getdate() as date)

The time component is probably the problem -- preventing a match between the date and the datetime. 时间部分可能是问题所在–阻止了日期和日期时间之间的匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM