简体   繁体   中英

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. These combo boxes make up the SQL statement in my code:

    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 . The SQL statement in the code will end up looking like : 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'" ). Also do I need to be opening the Access databse first in order to access the SQL server inside of it?

GlobalVariables.Date and GlobalVariables.Date2 both make up the date that I want to compare. The GlobalVariables.AfterBeforeTracker decides whether it will be a greater than, or less than sign

Seems the query you are building with the SqlCommand is wrong. Which date do you want to compare? GlobalVariables.DAte or date2? and what is that GlobalVariables.AfterBeforeTracker? Ideally your query could be something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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