0

我正在尝试从迁移到 Access 2010 的 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.Date两者GlobalVariables.Date2都构成了我要比较的日期。决定它GlobalVariables.AfterBeforeTracker是大于还是小于符号

4

1 回答 1

1

似乎您使用 SqlCommand 构建的查询是错误的。您想比较哪个日期?GlobalVariables.Date 还是 date2?什么是 GlobalVariables.AfterBeforeTracker?理想情况下,您的查询可能类似于

SqlCommand("SELECT * FROM dbo.b_Pulp_PI_Forte WHERE keyprinter_datetime <  '" + GlobalVariables.Date + "'");
于 2013-08-06T15:17:41.050 回答