簡體   English   中英

從SQL Server讀取數據“無數據”錯誤消息

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

我正在嘗試從遷移到Access 2010的SQL Server中讀取數據。在我的程序中,我有一個表格,里面滿是組合框,您可以選擇一個特定的日期來與SQL Server提取信息。 這些組合框構成了我的代碼中的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);
        }
    }

我收到的錯誤是“沒有數據時嘗試讀取無效”。 mycolumndata 代碼中的SQL語句最終看起來像: System.Data.SqlClient.SqlCommand 真的,我要問的是; 是什么導致SQL語句不具有我想要的值(我希望SQL語句值是: "SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime < '08/01/2013 04:00:00'" )。 另外,是否需要首先打開Access數據庫才能訪問其中的SQL Server?

GlobalVariables.DateGlobalVariables.Date2都構成了我要比較的日期。 GlobalVariables.AfterBeforeTracker決定它是大於還是小於符號

似乎您使用SqlCommand構建的查詢是錯誤的。 您想比較哪個日期? GlobalVariables.DAte還是date2? 那是什么GlobalVariables.AfterBeforeTracker? 理想情況下,您的查詢可能類似於

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