简体   繁体   English

从SQL Server读取数据“无数据”错误消息

[英]Reading data from SQL server “no data present” error message

I am trying to read data from a SQL server that is migrated into Access 2010. In my program, I have a form that full of combo boxes that you can choose a certain date that you can pull info from the SQL server with. 我正在尝试从迁移到Access 2010的SQL Server中读取数据。在我的程序中,我有一个表格,里面满是组合框,您可以选择一个特定的日期来与SQL Server提取信息。 These combo boxes make up the SQL statement in my code: 这些组合框构成了我的代码中的SQL语句:

    public void GetData()
    {
        try
        {
            //Connecting to access databse, then the SQL server.
            //Making the connection string.
            OleDbConnection DatabaseConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ForteSenderv2.0\TestServerDatabase.accdb");
            SqlConnection SQLConnection = new SqlConnection("Server=THIPSQLW01;Database=wss_test;Uid=baletrack;Pwd=BaleTrack;");

            //Openeing the database before opening the SQL server.
            DatabaseConnection.Open();
            //Opening the SQL Server.
            SQLConnection.Open();

            //Making the SQL statement, selecting everything form the data where specified.
            SqlCommand SQLstatement = new SqlCommand("SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime " + GlobalVariables.AfterBeforeTracker + " '" + GlobalVariables.Date + " " + GlobalVariables.Date2 + "'");

            //Initializing the connection properties.
            SQLstatement.CommandType = CommandType.Text;
            SQLstatement.Connection = SQLConnection;

            SqlDataReader TransferRecord = SQLstatement.ExecuteReader();

            //Setting each string to a string to tansfer to the .dat file.
            do
            {
                string mycolumndata = Convert.ToString(TransferRecord["bale_id"]);
            }
            while (TransferRecord.Read());

            //Closing the SQL server, and database connection.
            SQLConnection.Close();
            DatabaseConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(Environment.NewLine + "Retrieving data failure: " + ex.Message);
        }
    }

The error I am getting is "Invalid attempt to read when no data is present." 我收到的错误是“没有数据时尝试读取无效”。 at mycolumndata . mycolumndata The SQL statement in the code will end up looking like : System.Data.SqlClient.SqlCommand . 代码中的SQL语句最终看起来像: System.Data.SqlClient.SqlCommand So really, what I am asking is; 真的,我要问的是; what is causing the SQL statement to come up not having the value I want in it (I want the SQL statement value to be: "SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime < '08/01/2013 04:00:00'" ). 是什么导致SQL语句不具有我想要的值(我希望SQL语句值是: "SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime < '08/01/2013 04:00:00'" )。 Also do I need to be opening the Access databse first in order to access the SQL server inside of it? 另外,是否需要首先打开Access数据库才能访问其中的SQL Server?

GlobalVariables.Date and GlobalVariables.Date2 both make up the date that I want to compare. GlobalVariables.DateGlobalVariables.Date2都构成了我要比较的日期。 The GlobalVariables.AfterBeforeTracker decides whether it will be a greater than, or less than sign GlobalVariables.AfterBeforeTracker决定它是大于还是小于符号

Seems the query you are building with the SqlCommand is wrong. 似乎您使用SqlCommand构建的查询是错误的。 Which date do you want to compare? 您想比较哪个日期? GlobalVariables.DAte or date2? GlobalVariables.DAte还是date2? and what is that GlobalVariables.AfterBeforeTracker? 那是什么GlobalVariables.AfterBeforeTracker? Ideally your query could be something like 理想情况下,您的查询可能类似于

SqlCommand("SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime <  '" + GlobalVariables.Date + "'");

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

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