gpt4 book ai didi

c# - 在多个 select 语句上运行参数化查询

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:50 28 4
gpt4 key购买 nike

我有一段代码用于用新数据更新 Foxpro 表,但是为了获得这些数据我需要运行多个选择语句。我关于如何执行此操作的理论是使用参数化查询,但是我现在收到错误

Index was outside the bounds of the array

我假设这是因为我有多个 SELECT 语句,但是我无法通过连接运行它,因为这些表之间没有链接。

enter image description here

下面是连接字符串和原始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 处的字段的行。您似乎假设您有两个字段,因为您有两个选择。但这不是 OleDbDataReader 的工作方式。您的命令产生两组不同的数据,每组数据只有一个字段。第一个选择产生你的第一个结果,这是你正在循环的集合。

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

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

但是,查看您的代码,似乎每个表的记录数相同,并且两个表之间没有明显的关系。
在这种情况下(假设您没有要处理的大结果集)我可以简单地加载两个数据表,然后使用两个表的 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!");

关于c# - 在多个 select 语句上运行参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329814/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com