2013-08-06 46 views
0

我試圖從遷移到Access 2010的SQL服務器讀取數據。在我的程序中,我有一個表單,可以選擇組合框某個日期,你可以從SQL服務器上獲取信息。這些組合框組成的SQL語句在我的代碼:從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服務器?

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

回答

1

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

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

'GlobalVariables.Date和GlobalVariables.Date2'都構成了我想要比較的日期。 'GlobalVariables.AfterBeforeTracker'決定它是否會大於或小於符號。 –

+0

您是否在GlobalVariables.AfterBeforeTracker中看到了什麼值?您獲得的當前查詢是什麼?或者它出錯了? – Nilesh

+0

'GlobalVariables.AfterBeforeTracker'的值是**「<」**,但是SQL語句的整個值以'System.Data.SqlClient.SqlCommand'出現。它不會在SQL語句中出錯,它會在'mycolumndata'處出錯。我想要在那裏形成的價值是'850'。 –

相關問題