运行 多个 select 语句的参数化查询

Running parameterised queries on multiple select statments

我有一段代码用于用新数据更新 Foxpro table,但是为了获得这些数据我需要 运行 不止一个 select 语句。我关于如何做到这一点的理论是使用参数化查询,但是我现在收到错误

Index was outside the bounds of the array

我假设这是因为我有多个 SELECT 语句,但是我不能 运行 使用连接,因为这些 table 之间没有 link。

下面是连接字符串和原始Select语句

using (var importConnection = new OleDbConnection(
           connectionString: @"Provider=vfpoledb.1;
           Exclusive=false;
           data source=C:\Users\Joshua.cameron\Desktop\PCHomesImportTestBlank\PCHomesServer\DATABASE\pchomes.dbc")
      )
        using (OleDbCommand CodeChange = new OleDbCommand(
               @"select NUM        
                 from SYSTEMNUMBERS; 
                 select PROPCODE from PROPERTY order by PROPCODE", importConnection))

以及调用和更新的代码。

importConnection.Open();

            Console.WriteLine("Visual Foxpro connection open");

            // Initiate the reader to SQL
            var exportReader = CodeChange.ExecuteReader();

            // Start reading
            while (exportReader != null && exportReader.Read())
            {
                // Set parameter values whilst reading from SQL
                Int32 currentNum = Int32.Parse(exportReader.GetInt32(0).ToString());
                string propcode = exportReader.GetValue(1).ToString();
                currentNum = currentNum + 1;
                string padprop = currentNum.ToString().PadLeft(3);
                string propcode2 = "BIDME_" + padprop;
                // insert into VFP
                var propins = new OleDbCommand(@"update PROPERTY set PROPCODE=" + propcode2 + "where PROPCODE=" + propcode);
                var clientins = new OleDbCommand(@"update CLIENT set PROPCODET="+ propcode2 + "where PROPCODET=" + propcode);
                try
                {
                    propins.ExecuteNonQuery();
                }
                catch (Exception p)
                {
                    Console.Write("Error!");
                    Console.Write(p);
                    Console.Read();
                }
                try
                {
                    clientins.ExecuteNonQuery();
                }
                catch (Exception c)
                {
                    Console.Write("Error!");
                    Console.Write(c);
                    Console.Read();
                }


                try
                {
                    CodeChange.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.Write("Error Writing to database");
                    Console.Write(e);
                    Console.ReadKey();
                }
            }

            // done
            Console.WriteLine("Complete!");
            importConnection.Close();
        }

错误的确切原因是试图读取 DataReader 索引 1 处的字段的行。您似乎假设您有两个字段,因为您有两个 select。但这不是 OleDbDataReader 的工作方式。您的命令产生两组不同的数据,每组数据只有一个字段。第一个 select 产生你的第一个结果,这是你正在循环的集合。

您不能将两个结果连接在一起并在同一个循环中使用它们的值。您首先需要使用所有第一个结果,然后使用 OleDbDataReader 的 NextResult 方法传递给第二个结果,并启动另一个调用 Read() 的循环。

警告 我不确定 visual-foxpro 提供程序是否支持在同一命令中使用多个 select 语句。如果不是,那么您别无选择,只能发出两个单独的命令。

但是,查看您的代码,似乎每个 table 都有相同数量的记录,并且两个 table 之间没有明显的关系。
在这种情况下(假设您没有要处理的大结果集)我可以简单地加载两个数据 tables 然后使用两个 tables[=12 的 DataRows 处理您的更新=]

using (var importConnection = new OleDbConnection(....))
using (OleDbCommand CodeChange = new OleDbCommand(
   @"select NUM from SYSTEMNUMBERS; 
    select PROPCODE from PROPERTY order by PROPCODE", importConnection))
{
    importConnection.Open();
    DataTable sysNum = new DataTable();
    DataTable props = new DataData();
    Console.WriteLine("Visual Foxpro connection open");
    var exportReader = CodeChange.ExecuteReader();
    sysNum.Load(exportReader);
    exportReader.NextResult();
    props.Load(exportReader);
    for (int x = 0; x < sysNum.Rows.Count; x++)
    {
        // Set parameter values whilst reading from SQL
        Int32 currentNum = Int32.Parse(sysNum.Rows[i][0]);
        string propcode = props.Rows[i][0].ToString();

        .... continue with your current code ....
        .... but remove this part.....
//            try
//            {
//                CodeChange.ExecuteNonQuery();
//            }
//            catch (Exception e)
//            {
//                Console.Write("Error Writing to database");
//                Console.Write(e);
//                Console.ReadKey();
//            }


    }
}

// done
Console.WriteLine("Complete!");