在c#中,我尝试使用以下行插入和更新表:
OracleDataAdapter da = new OracleDataAdapter(query, connection);
da.Fill(ds);
这很有效。
但是,我需要知道如何在更新时插入行数。查询类似于
insert into someTable Values(...)
答案 0 :(得分:0)
如果不运行单独的count(*)查询,则无法知道记录集计数。这是设计的。 DataReader和DataAdapter是只读的,只读。
您可以尝试使用下面的替代方法来获取受影响的行。
public bool UploadBulkData(List<CustomerDTO> bulkData)
{
bool returnValue = false;
try
{
string query = @"insert into PCMS.Customer ( surname, firstName, emailAddress) values
(:surname, :firstName, :emailAddress)";
oracleConnection.Open();
using (var command = oracleConnection.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.BindByName = true;
// In order to use ArrayBinding, the ArrayBindCount property
// of OracleCommand object must be set to the number of records to be inserted
command.ArrayBindCount = bulkData.Count;
command.Parameters.Add(":surname", OracleDbType.Varchar2, bulkData.Select(c => c.Surname).ToArray(), ParameterDirection.Input);
command.Parameters.Add(":firstName", OracleDbType.Varchar2, bulkData.Select(c => c.FirstName).ToArray(), ParameterDirection.Input);
command.Parameters.Add(":emailAddress", OracleDbType.Varchar2, bulkData.Select(c => c.EmailAddress).ToArray(), ParameterDirection.Input);
int result = command.ExecuteNonQuery();
**result is what you need**
}
}
catch (OracleException ex)
{
//Log error thrown
}
finally
{
oracleConnection.Close();
}
return returnValue;
}